I’m working through the Django 1.4 tutorial, and I notice that in the view layer, URLs are assembled by hand:
<form action="/polls/{{ poll.id }}/vote/" method="post">
And so on.
However, on the controller side, URLs are generated dynamically:
return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
Something seems a little inconsistent about this. Since the prefix for the URL (/polls in this case) is determined in the project’s URLconf instead of the application’s URLconf, it seems unwise to hard-code this value in the template.
Is there a helper or some other way to generate a URL in a Django template so that it takes the URLconf into account?
Yes, you can use the builtin
url-tag to avoid hardcoding a path.For example: