In Django’s template language, you can use {% url [viewname] [args] %} to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?
What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it’s the current page or not). This is because it will be a lot cleaner to do this in Python than the template language.
If you need to use something similar to the
{% url %}template tag in your code, Django provides thedjango.core.urlresolvers.reverse(). Thereversefunction has the following signature:https://docs.djangoproject.com/en/dev/ref/urlresolvers/
At the time of this edit the import is
django.urls import reverse