'How to dynamically construct a hyperlink using Django named url

In my Django application I am using an ajax function to send some field values, do some processing (in views.py) and in the event of some exception (data mismatch etc), trying to construct a url for the user to use and navigate to the intended page (for further activities).

Now, in the event of the exception (as described above), I am sending a pre-configured exception message (var_err) and a url (var_err_url) that would be constructed into a hyperlink for the user to make use of.

Presently I am using the following scenario:

Passing the two variables as json data bits (to display message text and dynamically creating a hyperlink, respectively) to the template and using jQuery to display the message along with the hyperlink (as shown hereunder):

$('#id_err_message').html(var_err + ' <a href=" ' + var_err_url + '">Check</a>');

My set up works if I pass the url (variable var_err_url) as shown below:

'/movement/planning/tpt_tenders/create/' i.e. the complete path.

What I am trying to find out is:

Is there a way to, instead, pass (Django) url name and dynamically create the hyperlink to have the same effect as shown under (as is done in Django templates), i.e.:

<a href="{% url 'tpt_tender_add' %}">Add Tpt Tender </a>

Shall be grateful for a solution.

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source