Django:在包含 URL 时,我应该指定 project.app 还是仅指定 app
当在项目的 urls.py 中包含应用程序的 URL 时,我的编码合作伙伴会这样做:
('^stops/', include('stops.urls'))
但是,Django 文档 指定了以下语法:
('^clients/', include('project_name.app_name.urls'))
他的方法有效。是否有理由指定项目名称?
When including an app's URL's in the project's urls.py
, my coding partner does it this way:
('^stops/', include('stops.urls'))
However, Django documentation specifies the following syntax:
('^clients/', include('project_name.app_name.urls'))
His way has worked. Is there a reason to specify the project name at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您的
PYTHONPATH
设置以及项目和应用程序的结构。我们有很多很多的项目。每个都有多个应用程序。所有这些都在我们的
PYTHONPATH
上,因此项目名称至关重要。如果您只有一个项目,并且顶级项目目录位于您的
PYTHONPATH
上,那么每个应用程序都可以单独解析,并且您不能使用项目名称。It depends on your
PYTHONPATH
setting and the structure of your projects and apps.We have many, many projects. Each with several apps. All are on our
PYTHONPATH
, so the project name is essential.If you have only one project, and the top-level project directory is on your
PYTHONPATH
, then each app can be resolved separately and you can't use the project name.它还取决于该应用程序是否在您的项目中,或者是可重用的项目。
我为每个项目都有一个新的 virtualenv,并为每个应用程序使用单独的 Mercurial 存储库。然后将它们安装到系统路径中(以可编辑的形式进行开发,或以不可编辑的形式进行部署),这意味着我在
PYTHONPATH
上有
。It also depends upon if the app is inside your project, or a reusable one.
I have a fresh virtualenv for every project, and use a separate mercurial repository for each app. These are then installed into the system path (either in editable form for development, or in non-editable form for deployment), meaning I have
<appname>
on thePYTHONPATH
.如果在 shell 上运行,
您将看到 python 的禅宗“显式优于隐式”,因此这就是指定项目名称的原因。
if on the shell you run
you will see that there is a zen of python ''explicit is better than implicit'' hence thats the reason to specify the project name.