3

我是 Django 新手,面临下一个问题:当我打开相应的链接时,我收到下一个错误:

NoReverseMatch at /tutorial/

Reverse for 'tutorial.views.section_tutorial' with arguments '(1L,)' and keyword arguments '{}' not found.

我究竟做错了什么?为什么在参数中传递“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 %}
4

1 回答 1

4

$包含应用程序 url 时,您的主 urls 文件中的 url 正则表达式中不需要标识符:

url(r'^tutorial/$', include('apps.tutorial.urls')),

应该:

url(r'^tutorial/', include('apps.tutorial.urls')),
于 2013-10-14T16:10:17.430 回答