Django URL不起作用:NoReverseMatch错误

时间:2015-01-20 17:19:15

标签: python html django

我刚刚改变了我的应用程序中的一些URL,当我尝试渲染索引页面时,我收到此错误。有人能告诉我这是什么问题吗?我检查了我的html文件中的拼写错误等。错误如下:

Reverse for 'video_list' with arguments '()' and keyword arguments '{}' not  found. 1 pattern(s) tried: [u'website/video/(?P<pk>[a-z\\d]+)/$']

我在我的应用程序的url.py中有以下内容:

import views
admin.autodiscover()

video_detail = DetailView.as_view(model=Video)  
video_list = ListView.as_view(model=Video)

urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^website/(?P<pk>[a-z\d]+)/$', views.VideoListView.as_view(),  name='video_list'),
    url(r'^website/(?P<pk>[a-z\d]+)/$', video_detail, name='video_detail'),
    url(r'^website/(?P<pk>[a-z\d]+)/$', video_detail, name='video_recording'),
    url(r'^(?P<video_id>[a-z\d]+)/post_comment/$', views.post_comment, name='post_comment'),
)

我的index.html有,

{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'website/style.css' %}" />

<head>
<h1> Title </h1>

<style>

ul {
    margin: 0;
    padding: 0;
}

li {
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica   Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
    font-weight: normal;
    font-size: 20px;
    display: inline;
    color: green;
}
</style>
</head>  
<body>

<ul>
  <li><a href={% url 'website:video_list'%}>{{ "View Videos" }}</a></li>
  <li><a href="#news">Record Video</a></li>
</ul>
</body>

堆栈跟踪是:

Traceback:
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  447.                         six.reraise(*exc_info)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  433.             url = reverse(view_name, args=args, kwargs=kwargs,  current_app=context.current_app)
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  536.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/Anupa/myproject/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  456.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /website/
Exception Value: Reverse for 'video_list' with arguments '()' and keyword   arguments '{}' not found. 1 pattern(s) tried: [u'website/video/(?P<pk>[a-   z\\d]+)/$']

1 个答案:

答案 0 :(得分:4)

您正在模板中执行此操作:

{% url 'website:video_list'%}

...但你的网址有:

url(r'^website/(?P<pk>[a-z\d]+)/$', views.VideoListView.as_view(),  name='video_list'),

换句话说,您的网址中的pk arg是必需的,但是您要求Django在没有指定任何pk的情况下撤消网址。

您可能不希望列表视图的网址中有pk段,因此您的urlconf应如下所示:

url(r'^website/$', views.VideoListView.as_view(),  name='video_list'),
url(r'^website/(?P<pk>[a-z\d]+)/$', video_detail, name='video_detail'),