【问题标题】:URL name keeps appending to end DjangoURL 名称一直附加到结束 Django
【发布时间】:2015-05-21 19:17:11
【问题描述】:

我遇到了一些关于 Django 中的 URL 映射的问题。我有以下代码:

table.html:

<form id="filter_form" method="post" action="update_filters/">
    <input type="submit" name="submit" value="Report" />
</form>

urls.py:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^update_filters/', views.filter_report, name='update_filters'),
]

views.py:

def filter_report(request):
    # Code in the function
    return render(request, 'autotester/table.html', context)

一切正常,但是当我多次点击“报告”按钮时,我得到:

127.0.0.1:8000/autotester/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters/update_filters  
etc

我不知道是什么原因造成的。必须有某种简单的解决方法,但我就是找不到它,我已经尝试了 3 个小时来解决这个问题,但我的大脑只是被炒了。

【问题讨论】:

    标签: python django url-mapping


    【解决方案1】:

    尝试使用{% url 'update_filters' %} 模板标签。并在你的 url 定义中的正则表达式末尾添加$

    url(r'^update_filters/$', views.filter_report, name='update_filters'),
    

    【讨论】:

    • 做到了。你知道为什么会修复它吗?或者你能指点我一个解释它的参考吗?我只是想明白。
    • $ 符号表示 URL 必须在此处结束,并且不允许在其后附加任何内容。
    • 哦,好吧,但我的意思是为什么{% url 'update_filters' %} 停止了我的问题。我之前在那里有$,但是当我多次点击 Report 按钮时,它会抛出一个错误,说它不知道 autotester/update_filters/update_filters 在哪里是
    • 为什么在这里:如果你写 &lt;form id="filter_form" method="post" action="/update_filters/"&gt; 它应该可以工作。您在开始时缺少/,而是{% url 'update_filters' %} 将以正确的方式打印它。
    猜你喜欢
    • 2015-03-18
    • 2015-12-11
    • 2017-11-17
    • 2013-08-10
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多