0

I found a way to inject variables into the URL per Django Template: Add Variable Into URL As Parameter, but my reverse match is not working. Do I need to use re-path and use regular expressions?

Current url.py

    path('student/item/drilldown/<str:type>/<int:num>/',views.StudentBehaviorListView.as_view(),name='student_item_list_drilldown'),

Original w/hard-coding:

<a href="{%url 'registration:student_item_list_drilldown' type='grade' num=6%}">

With variable injection:

<a href="{%url 'registration:student_item_list_drilldown'%}?type=grade&num={{i.grade}}">{{i.grade}}th Grade</a>

Error message:

NoReverseMatch at /registration/dashboard/
Reverse for 'student_item_list_drilldown' with no arguments not found. 1 pattern(s) tried: ['registration/student/item/drilldown/(?P<type>[^/]+)/(?P<num>[0-9]+)/\\Z']

1 Answer 1

0

This:

{% url 'registration:student_item_list_drilldown' %}

Will try to find path named student_item_list_drilldown before it is read as html. That's why it tries (and fails) to find <str:type> and <int:num> in {% url 'registration:student_item_list_drilldown' %}. You have to include that variables inside {% url ... %} brackets.

The best approach would be that:

{% url 'registration:student_item_list_drilldown' 'grade' i.grade %}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. Can you suggest how I add in the i.grade variable in here?
I've edited. {% url 'registration:student_item_list_drilldown' 'grade' i.grade %} should work like a charm :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.