Brant
Brant

Reputation: 6031

Django: Best Practice for URL conf, url template tag

WIth class-based views having become MUCH better in Django, I am running into a "best practices" problem when implementing a class based view. It basically comes down to the URL template tag.

Given a urls.py like this:

urlpatterns = patterns('some_app.views', 
    url(r'^$', 'index', name='some_app_index')
)

That tag can take either a path to a view:

{% url some_app.views.index %}

or the name of a url:

{% url some_app_index %}

Now, with a class-based url conf, one ends up with a url like this:

from some_app.views import Index

urlpatterns = patterns('', 
    url(r'^$', Index.as_view(), name='some_app_index')
)

Which means that using {% url some_app.views.index %} no longer works but {% url some_app_index %} still does. (And {% url some_app.views.Index.as_view %} doesn't seem to be a solution).


So, my question is, what is best practice for refering to URL confs from a template?

To this point, I beleived that using the path.to.view method was better, since it was cleanly namespaced. However, with class-based views looking better and better, is using the url name a better way to go? In that case, namespacing is completely dependent on the name attribute being setup by the app developer in a way that separates the url name from other apps...

Thoughts? I couldn't find a "do it this way" in the Django documentation but if anyone has written about this, I'd love to read it.

Upvotes: 5

Views: 2200

Answers (1)

user391538
user391538

Reputation:

I always use names.

Besides the problem you mention with paths, you would also have a problem if you have two URLs pointing to the same view.

Upvotes: 8

Related Questions