<p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p>

<p>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.</p>

[{'answer_id': 43312, 'author': 'Peter Hoffmann', 'author_id': 720, 'author_profile': 'https://Stackoverflow.com/users/720', 'pm_score': 7, 'selected': True, 'text': '<p>If you need to use something similar to the <code>{% url %}</code> template tag in your code, Django provides the <code>django.core.urlresolvers.reverse()</code>. The <code>reverse</code> function has the following signature:</p>\n\n<pre><code>reverse(viewname, urlconf=None, args=None, kwargs=None)\n</code></pre>\n\n<p><a href="https://docs.djangoproject.com/en/dev/ref/urlresolvers/" rel="noreferrer">https://docs.djangoproject.com/en/dev/ref/urlresolvers/</a></p>\n\n<p>At the time of this edit the import is <code>django.urls import reverse</code></p>\n'}, {'answer_id': 55734, 'author': 'Garth Kidd', 'author_id': 5700, 'author_profile': 'https://Stackoverflow.com/users/5700', 'pm_score': 4, 'selected': False, 'text': '<p>I\'m using two different approaches in my <code>models.py</code>. The first is the <code>permalink</code> decorator:</p>\n\n<pre><code>from django.db.models import permalink\n\ndef get_absolute_url(self): \n    """Construct the absolute URL for this Item."""\n    return (\'project.app.views.view_name\', [str(self.id)])\nget_absolute_url = permalink(get_absolute_url)\n</code></pre>\n\n<p>You can also call <code>reverse</code> directly:</p>\n\n<pre><code>from django.core.urlresolvers import reverse\n\ndef get_absolute_url(self): \n    """Construct the absolute URL for this Item."""\n    return reverse(\'project.app.views.view_name\', None, [str(self.id)])\n</code></pre>\n'}, {'answer_id': 72249, 'author': 'zgoda', 'author_id': 12138, 'author_profile': 'https://Stackoverflow.com/users/12138', 'pm_score': 2, 'selected': False, 'text': '<p>Be aware that using <code>reverse()</code> requires that your urlconf module is 100% error free and can be processed - iow no <code>ViewDoesNotExist</code> errors or so, or you get the dreaded <code>NoReverseMatch</code> exception (errors in templates usually fail silently resulting in <code>None</code>).</p>\n'}, {'answer_id': 57151646, 'author': 'juan Isaza', 'author_id': 2394901, 'author_profile': 'https://Stackoverflow.com/users/2394901', 'pm_score': 3, 'selected': False, 'text': "<p>For Python3 and Django 2:</p>\n<pre><code>from django.urls import reverse\n\nurl = reverse('my_app:endpoint', kwargs={'arg1': arg_1})\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/43290', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3355/']