Как передать переменную в блок Django в url?
Контекст
Я пытаюсь просмотреть список объектов, затем вызвать атрибуты name и id и передать их в блок Url в виде regex, чтобы затем перехватить их в urls.py.
Проблема
{%if people_found%}
{%for the_people in people_found%}
<a href="{% url 'Search4Bday:profile' '{{the_people.firstname}}/ {{the_people.id}}' %}">{{the_people.firstname}}</a>
{%endfor%}
{%endif%}
Urls.py
app_name = 'Search4Bday'
urlpatterns= [path("search/profile/<str:firstname>/<int:id>", views.profile, name = "profile")]
Вопрос
Я не уверен, как я могу передать переменные в файл urls.py, если я не могу обращаться к переменной the_people с помощью двойных фигурных скобок. Я также не уверен, что это правильный способ передачи переменных в часть urls.py, предназначенную для перехвата регулярных выражений. Есть идеи?
Используйте тег add из Django Template Lang:
{% if people_found %}
{% for the_people in people_found %}
<a href="{% url 'Search4Bday:profile' the_people.firstname|add:"/"|add:the_people.id %}">{{the_people.firstname}}</a>
{% endfor %}
{% endif %}
Я попробовал другой метод, я мог бы просто сделать вот так:
{% if people_found %}
{% for the_people in people_found %}
<a href="{% url 'Search4Bday:profile' the_people.firstname the_people.id %}">{{the_people.firstname}}</a>
{% endfor %}
{% endif %}
https://docs.djangoproject.com/en/4.0/ref/templates/language/