Django:URL conf、url 模板标签的最佳实践
随着基于类的视图在 Django 中变得更好,我在实现基于类的视图时遇到了“最佳实践”问题。它基本上归结为 URL 模板标签。
给定一个像这样的 urls.py:
urlpatterns = patterns('some_app.views',
url(r'^$', 'index', name='some_app_index')
)
该标签可以采用视图的路径:
{% url some_app.views.index %}
或 url 的名称:
{% url some_app_index %}
现在,使用基于类的 url conf,最终会得到这样的 url:
from some_app.views import Index
urlpatterns = patterns('',
url(r'^$', Index.as_view(), name='some_app_index')
)
这意味着使用 < code>{% url some_app.views.index %} 不再有效,但 {% url some_app_index %}
仍然有效。 (并且 {% url some_app.views.Index.as_view %}
似乎不是一个解决方案)。
所以,我的问题是,从模板引用 URL conf 的最佳实践是什么?
到目前为止,我相信使用 path.to.view 方法更好,因为它是干净的命名空间。然而,随着基于类的视图看起来越来越好,正在使用 网址名称 更好的方法吗?在这种情况下,命名空间完全取决于应用程序开发人员以将 url 名称与其他应用程序分开的方式设置的名称属性......
有什么想法吗?我在 Django 文档中找不到“这样做”,但如果有人写过有关此内容的文章,我很乐意阅读。
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'^
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'^
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.
, 'index', name='some_app_index')
)
That tag can take either a path to a view:
or the name of a url:
Now, with a class-based url conf, one ends up with a url like this:
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.
, 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.
, 'index', name='some_app_index') )That tag can take either a path to a view:
or the name of a url:
Now, with a class-based url conf, one ends up with a url like this:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我总是用名字。
除了您提到的路径问题之外,如果您有两个指向同一视图的 URL,您还会遇到问题。
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.