Django url反向匹配错误

时间:2013-05-01 16:32:56

标签: python django heroku

这是我的应用程序树:

myapp/
  assets/
  statics/
  settings.py
  urls.py
  \\other stuff
main/
  urls.py
  views.py
  templates/
     base.html
     index.html
  \\other stuff
manage.py

测试我的应用程序时,出现以下错误:

Error during template rendering

In template /app/main/templates/base.html, error at line 32
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.

以下是错误中涉及的base.html中的代码:

{% load i18n %}
\\some stuff

 <a class="brand" href="{% url 'index' %}">{{ site.name }}</a> <--- this line is the error

main/urls.py我有:

urlpatterns = patterns('',
  (r'^$', TemplateView.as_view(template_name="index.html")),
)

myapp/urls.py我有:

urlpatterns = patterns('',
                       url(r'', include('main.urls'), name='index'),

)

有人可以告诉我我做错了什么以及为什么姓名'index'没有反向匹配?

1 个答案:

答案 0 :(得分:3)

name用于单个视图,它不与include语句一起使用。试试这个:

在main / urls.py中:

urlpatterns = patterns('',
                       url(r'^$', TemplateView.as_view(template_name="index.html"), 
                           name='index'),
)

在myapp / urls.py中:

urlpatterns = patterns('',
                       url(r'', include('main.urls')),
)