Search code examples
pythondjangodjango-viewsdjango-templatesdjango-urls

How to use URL patterns in a HTML file using Django?


I am creating a web application in Django, and I need to link to a webpage that creates a model and then redirects you back.

I have already created this webpage, but I need to create a link to it. This is the URL pattern for it (yes, it is long and ugly):

path('add_to_trip/<int:id>/<str:name>/<str:addr>/<str:rating>', add_to_trip, name="add_to_trip"),

I am trying to use this link to redirect to it, but Django can't understand it.

<a class="btn btn-primary" href="{% url 'add_to_trip/{{trip_id}}/{{place_results_name}}/{{place_results_addr}}/{{place_results_rating}}' %}" style="margin-top: 10px;">Add to trip</a>

This is the error I am getting:

NoReverseMatch at /plan/3/ Reverse for 'add_to_trip/{{trip_id}}/{{place_results_name}}/{{place_results_addr}}/{{place_results_rating}}' not found. 'add_to_trip/{{trip_id}}/{{place_results_name}}/{{place_results_addr}}/{{place_results_rating}}' is not a valid view function or pattern name.

I don't know why Django isn't using the URl pattern I provided.


Solution

  • For that url:

    path('add_to_trip/<int:id>/<str:name>/<str:addr>/<str:rating>', add_to_trip, name="add_to_trip"),
    

    You should use such syntax:

    <a class="btn btn-primary" href="{% url 'add_to_trip' id=trip_id name=place_results_name addr=place_results_addr rating=place_results_rating %}" style="margin-top: 10px;">Add to trip</a>