It gets less nice when you have multiple apps on the same project, and you start namespacing the URLs. Then, to reverse them, you need to add the namespace to the URL reversal tags:
{% url 'mypoll:detail' question.id %}
Ewww. You really want to use ‘detail’ without the namespacing.
There is a solution, and it’s that the URL reversal has a somewhat complex way of matching the name.
It says if there’s no match to a namespace, it’ll match to the app name.
So you can use the appname as a kind of generic namespace:
{% url 'polls:detail' question.id %}
Then, you need to add an attribute ‘current_app’ that is set to the app name. That’s when it gets tricky. The example code at the link has one way. Here’s another example, using the tutorial’s example:
class DetailView(generic.DetailView): model = Question def get(self, request, *args, **kwargs): request.current_app = self.request.resolver_match.namespace return super(DetailView, self).get(self, request, *args, **kwargs)
There’s a lot of repeated code.
I’m starting to think this is mainly an aesthetic problem, not a real-world problem. There are probably few situations when you want really generic URL reversal.
“Oh well” for now.