不能有任何带有 slug 的 URL。无反向匹配
Cannot have any URLs with slugs. NoReverseMatch
我是一个初学者,在处理 django slug url 系统和这些 NoReverseMatch 错误时遇到了困难,即使在阅读了文档之后,这些错误对我来说也毫无意义。
我有一个 Django 项目。在其中一个视图中,我将 geoJSON 特征列表传递到模板中,并在地图上显示它们。我想让每个功能都充当一个可点击的 'link' 到一个将显示它的内容的视图。以下是具有我要单击的那些功能的模板的一部分:
//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
});
}
(我已经确认 feature.properties.myslug 确实包含我想要的 slug)。
我想去的url模式:
urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]
及其相关的观点:
def AreaDetail(request, areaslug):
area = get_object_or_404(Area, nameslug=areaslug)
return render(request, 'polls/areadetail.html', {'area':area})
我遇到的问题是,通过执行我展示的操作并将 url 引用放置在我上面展示的模板中,我希望能够点击,该模板甚至根本无法工作,给我 'Error during template rendering' 整页错误信息,开头为:
NoReverseMatch at /polls/areas/
Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']
任何帮助将不胜感激
编辑第 1 部分:正如我在回应 faltru 时所说的,我确信 feature.properties.myslug 实际上有一个 slug 表达式。
EDIT2:根据我在 django 票证中发现的内容,我在 urls.py 的 url 正则表达式中对 (?P<areaslug>[-\w]+)/$
进行了细微更改到 (?P<areaslug>[-\w]+)?/$
现在错误是:
Page not found (404)
Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail
有没有可能因为 "{% url 'polls:areadetail' feature.properties.myslug%}"
位在 javascript 里面,所以 feature.properties.myslug 没有被正确插入?好像这里需要某种括号?
根据错误信息,feature.properties.myslug
为空或没有值。
确保 feature.properties.myslug
从视图中正确传递。
- 暂时注释掉
{% url .. %}
。
- 打印
{{ feature }}
、{{ feature.properties }}
、{{ feature.properties.myslug }}
看有没有漏掉的部分。
- 相应地修复视图。
- 取消注释
{% url .. %}
。
经过更多的挖掘后,我在另一个问题中找到了为什么这不起作用的答案:
How to pass javascript variable to django custom filter
Ludwik Trammer 的回答是:
Django templates are build on the server side, while JavaScript is executed on the client side.
That means that template code is always executed before JavaScript (as
it is executed by the server, before the page is sent to the client).
As a consequence it is absolutely impossible to mix JavaScript and
Django code the way you want to.
这显然适用于此。我专注于 URL 模板的问题,urls.py 文件中的正则表达式等问题,无论我做了什么,因为它在 javascript 部分,运行 客户端,无论我做什么,URL 模板总是不完整的,因此无法解决我想要的问题。
我是一个初学者,在处理 django slug url 系统和这些 NoReverseMatch 错误时遇到了困难,即使在阅读了文档之后,这些错误对我来说也毫无意义。
我有一个 Django 项目。在其中一个视图中,我将 geoJSON 特征列表传递到模板中,并在地图上显示它们。我想让每个功能都充当一个可点击的 'link' 到一个将显示它的内容的视图。以下是具有我要单击的那些功能的模板的一部分:
//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
});
}
(我已经确认 feature.properties.myslug 确实包含我想要的 slug)。
我想去的url模式:
urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]
及其相关的观点:
def AreaDetail(request, areaslug):
area = get_object_or_404(Area, nameslug=areaslug)
return render(request, 'polls/areadetail.html', {'area':area})
我遇到的问题是,通过执行我展示的操作并将 url 引用放置在我上面展示的模板中,我希望能够点击,该模板甚至根本无法工作,给我 'Error during template rendering' 整页错误信息,开头为:
NoReverseMatch at /polls/areas/ Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']
任何帮助将不胜感激
编辑第 1 部分:正如我在回应 faltru 时所说的,我确信 feature.properties.myslug 实际上有一个 slug 表达式。
EDIT2:根据我在 django 票证中发现的内容,我在 urls.py 的 url 正则表达式中对 (?P<areaslug>[-\w]+)/$
进行了细微更改到 (?P<areaslug>[-\w]+)?/$
现在错误是:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail
有没有可能因为 "{% url 'polls:areadetail' feature.properties.myslug%}"
位在 javascript 里面,所以 feature.properties.myslug 没有被正确插入?好像这里需要某种括号?
根据错误信息,feature.properties.myslug
为空或没有值。
确保 feature.properties.myslug
从视图中正确传递。
- 暂时注释掉
{% url .. %}
。 - 打印
{{ feature }}
、{{ feature.properties }}
、{{ feature.properties.myslug }}
看有没有漏掉的部分。 - 相应地修复视图。
- 取消注释
{% url .. %}
。
经过更多的挖掘后,我在另一个问题中找到了为什么这不起作用的答案:
How to pass javascript variable to django custom filter
Ludwik Trammer 的回答是:
Django templates are build on the server side, while JavaScript is executed on the client side. That means that template code is always executed before JavaScript (as it is executed by the server, before the page is sent to the client). As a consequence it is absolutely impossible to mix JavaScript and Django code the way you want to.
这显然适用于此。我专注于 URL 模板的问题,urls.py 文件中的正则表达式等问题,无论我做了什么,因为它在 javascript 部分,运行 客户端,无论我做什么,URL 模板总是不完整的,因此无法解决我想要的问题。