开发者

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 basical开发者_如何学Goly 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜