【问题标题】:Django - Linking to web sections with buttons in a home pageDjango - 使用主页中的按钮链接到网页部分
【发布时间】:2013-10-07 12:13:57
【问题描述】:

大家好,感谢阅读!

我是一个非常新颖的 Django 用户。我决定从我的基本 web 转移到 django 来学习如何使用框架 web。我完成了 django 教程,但在我的项目中……这种方式一点也不轻松。

首先让我简要介绍一下我的项目,以便您可以大致了解整个项目。我的基本网站由主页和部分组成,您可以在其中更改不同的参数(例如室温)或检查传感器数据。

到现在为止,我的主页和温度控制部分正在运行(需要修复的东西很少,但渲染正确)。我想用主页中的按钮链接每个部分。这是我的主页模板:

{% extends 'base.html' %}
{% block content %}
<h1>Here goes a title</h1>
    <p>A welcome message here...</p>
    <h2>please select an action</h2>                                                                
    <div id="column_left" class="column">                                                         
    {% for action in actions_list_left %}                                                     
        <div id="action_box1{{ forloop.counter }}" class="action_box">                            
            <input type="button" onclick= " HELP HERE!! "                   
            id= "{{ action.action_name }}_sel" class="action_button">                             
            <div id="{{ action.action_name }}_desc" class="action_desc">                          
                {{ action.description }}                                                          
            </div>                                                                                
        </div>
    {% endfor %}
    </div>                                                                                          
<!--I ommit the right column, containing more actions...-->
{% endblock %}

我想使用我的“操作”模型字段链接到其他部分。我的想法是使用动作名称字段作为该部分的 url(以通用“模型”方式进行)。例如,如果我的主页位于 localhost:8000/ 并且操作名称为“temp”,我希望关联的按钮链接到 localhost:8000/temp 并呈现合适的模板。

因为我认为是 urls.py 问题,所以我在这里发布了我的 urls 文件(没有导入行)...

urls.py

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),                                                    
    url(r'^$', include('principal.urls')),
    url(r'^temp/', include('temp.urls')),     
)

principal/urls.py

urlpatterns = patterns('',
    url(r'^$', 'principal.views.home', name='home'),
)

temp/urls.py

urlpatterns = patterns('',
url(r'^$', 'temp.views.index', name='temp_index'),
     url(r'^(?P<pk>\d+)/$',
     DetailView.as_view(
         model=Room,
         template_name='temp/roomTemp.html')),
)

我尝试了一些我在其他帖子中阅读过的想法...

{% url temp:index %}

这个引发了 NoReverseMatch 错误...“temp”不是注册的命名空间。然后我用谷歌搜索了如何解决这个问题,并在 urls.py 中更改了这一行:

url(r'^temp/', include('temp.urls', namespace='temp')),

我也在 temp/urls.py 中更改了行:

url(r'^$', 'temp.views.index', name='temp'),

两个页面仍然可以正常渲染,但是按钮不起作用(什么都不做)......它一定很简单,但所有 url 的东西对我来说似乎有点棘手(顺便说一句,语法有时就像生成文件 mordor 文件 :P)。我正在使用 Django 1.3

任何帮助将不胜感激。此外,任何“优秀的 django 编码器”评论或解释都会非常有用,以免再次犯同样的错误!

提前致谢,

胡安

编辑: 我在此处的代码中发布了修复,以防有人发现它有用:

<form action="{% url action.action_url %}">
    <input type="submit" id="{{ action.action_name }}_sel" value="" class="action_button">
    <div id="{{ action.action_name }}_desc" class="action_desc">
        {{ action.description }}
    </div>
</form>

【问题讨论】:

    标签: django python-2.7 django-urls


    【解决方案1】:

    如果你不需要使用 url 命名空间,你可以这样做

    urls.py

    admin.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/', include(admin.site.urls)),                                                    
        url(r'^$', include('principal.urls')),
        url(r'^temp/', include('temp.urls')),     
    )
    

    temp/urls.py

    urlpatterns = patterns('',
    url(r'^$', 'temp.views.index', name='temp_index'),
        url(r'^(?P<pk>\d+)/$',
            DetailView.as_view(
            model=Room,
            template_name='temp/roomTemp.html')),
    )
    

    然后在模板中

    {% url 'temp_index' %}
    

    希望它对你有用。

    【讨论】:

    • 您好 esauro,首先,感谢您的尝试!这与我的第一个猜测非常相似......但我很抱歉说这不起作用。它返回一个模板语法错误(NoReverseMatch while rendering: Reverse for ''temp_index'' with arguments '()' and keyword arguments '{}' not found.)
    • 您使用的是哪个版本的 Django?这应该适用于 Django >= 1.5。如果您使用的是以前的版本,您需要在 {% extends 'base.html' %} 之后包含 {% load url from future %} 或删除引号 ({% url temp_index %})
    • 您好 esauro,感谢您的帮助。最后我明白了!我使用的是 django 1.3,所以你说的两个选项都对我有用。现在我正在尝试使用 action.action_name 来链接所有部分。完成后我会发布代码,以防其他人感兴趣。再次感谢。
    • 好吧,事实上我不能在 {% url %} 中使用变量,所以我正在尝试其他方法而不是修复它...
    • 我终于让它工作了......我已经在问题的末尾发布了代码。起初,我在使用 django 1.3 时成功地使用 '' 和 url_from_future 调用了 'temp_index' ;),然后我使用了变量。再次感谢您的帮助,esauro!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2012-01-15
    • 2013-03-07
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多