简体   繁体   中英

Django url reverse matching error

This is my application tree :

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

When testing my application, I have the following error :

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.

Here is the code in base.html involved in the error :

{% load i18n %}
\\some stuff

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

in main/urls.py I have :

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

in myapp/urls.py I have :

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

)

Can someone please tell my what I am doing wrong and why the name 'index' is not reverse matched?

name is for single views, it doesn't go with the include statement. Try this way:

in main/urls.py:

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

in myapp/urls.py:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM