F.M.F.
F.M.F.

Reputation: 2320

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?

Upvotes: 0

Views: 104

Answers (1)

wencakisa
wencakisa

Reputation: 5968

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.

Upvotes: 2

Related Questions