我是 Django 的新手,面临下一个问题:当我打开相应的链接时,出现下一个错误:NoReverseMatch at /tutorial/
Reverse for 'tutorial.views.section_tutorial' with arguments '(1L,)' and keyword arguments '{}' not found.
我究竟做错了什么?为什么在 args 中传递的是“1L”而不是“1”? (当我返回“1”时,我得到同样的错误。)我试图在我的模板中更改 'tutorial.views.section_tutorial'
的 'section-detail'
但仍然没有任何改变。使用 django 1.5.4,python 2.7;谢谢!tutorial/view.py
:
def get_xhtml(s_url):
...
return result
def section_tutorial(request, section_id):
sections = Section.objects.all()
subsections = Subsection.objects.all()
s_url = Section.objects.get(id=section_id).content
result = get_xhtml(s_url)
return render(request, 'tutorial/section.html', {'sections': sections,
'subsections': subsections,
'result': result})
tutorial/urls.py
:from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.main_tutorial, name='tutorial'),
url(r'^(?P<section_id>\d+)/$', views.section_tutorial, name='section-detail'),
url(r'^(?P<section_id>\d+)/(?P<subsection_id>\d+)/$', views.subsection_tutorial, name='subsection-detail'),
)
urls.py
:urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tutorial/$', include('apps.tutorial.urls')),
)
main.html
:{% extends "index.html" %}
{% block content %}
<div class="span2" data-spy="affix">
<ul id="menu">
{% for section in sections %}
<li>
<a href="{% url 'tutorial.views.section_tutorial' section.id %}">{{ section.name }}</a>
<ul>
{% for subsection in subsections%}
{% if subsection.section == section.id %}
<li><a href=#>{{ subsection.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</li>
</ul>
</div>
<div class="span9">
<div class="well">
{% autoescape off%}
{{ result }}
{% endautoescape %}
</div>
</div>
{% endblock %}
最佳答案
包含应用程序 url 时,您的主 urls 文件中的 url regex 中不需要 $
标识符:
url(r'^tutorial/$', include('apps.tutorial.urls')),
应该:
url(r'^tutorial/', include('apps.tutorial.urls')),
关于python - 使用参数 '' 和关键字参数 '(1L,)' 未找到反向 '{}',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19364340/