简体   繁体   中英

Django: template url tag wrong target

I have in my main urls:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('scoring.urls', namespace='scoring')),
]

and in my app urls:

urlpatterns = [
    url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
    url(r'^scoring/$', views.index, name='index'),
]

and in my template:

<li><a href="{% url 'scoring:index' %}">Scoring</a></li>

but what {% url 'scoring:index' %} generates is localhost/ instead of localhost/scoring . Why?

At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]

Then in your scoring urls.py make sure to add app_name :

app_name = 'scoring'

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]

(Note that I removed the scoring prefix in the url patterns.)

Now, as you've added app_name , the reversing in your template should work as expected.

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