使用Django,有没有更好的方法在urls.py中动态导入视图?
摘要:
假设我在 Django 中有一个名为“devsite”的项目,它将首先部署到临时项目(也称为“devsite”),最后部署到实时代码库(其中该项目称为“livesite”)。在实时部署期间,我必须手动更改 urls.py 才能从正确的项目导入视图。这意味着“devsite”中的 urls.py 将使用类似以下内容的内容:
from devsite import views
“livesite”的 urls.py 将更改为:
from livesite import views
我的解决方案:
以下内容似乎有效(到目前为止进行了有限的测试)。我所做的是在 settings.py 中创建一个变量以从目录中获取项目名称,如下所示:
settings.py
# /settings.py
import os.path
PROJECT_NAME = os.path.basename(os.path.dirname(__file__))
然后使用它在 urls.py 中导入正确的视图:
< b>urls.py
# /urls.py
from django.conf import settings
website = __import__('%s' % settings.PROJECT_NAME, fromlist=['views'])
...
urlpatterns = patterns('',
(r'^monty/$', website.views.monty),
)
我的问题:
我想知道的是:
- 这是做我想做的事情的好方法,还是有更好的方法来编码?
- 或者我是否需要重新考虑我的整个部署工作流程?
提前致谢。
Summary:
Say I have a project in Django called "devsite" which will be deployed first to a staging project (also called "devsite") and finally to the live codebase (where the project is called "livesite"). During live deployments, I'd have to make manual changes to urls.py in order to import views from the right project. Which means urls.py in "devsite" would use something like:
from devsite import views
And urls.py for "livesite" would be changed to:
from livesite import views
My Solution:
The following seems to work (with limited testing so far). What I've done is create a variable in settings.py to get the project name from the directory, like so:
settings.py
# /settings.py
import os.path
PROJECT_NAME = os.path.basename(os.path.dirname(__file__))
And then use this to import the correct views in urls.py:
urls.py
# /urls.py
from django.conf import settings
website = __import__('%s' % settings.PROJECT_NAME, fromlist=['views'])
...
urlpatterns = patterns('',
(r'^monty/
My Question:
What I'd like to know is:
- Is this a good way of doing what I want to do, or is there a better way to code this?
- Or do I need to rethink my whole deployment workflow?
Thanks in advance.
, website.views.monty),
)
My Question:
What I'd like to know is:
- Is this a good way of doing what I want to do, or is there a better way to code this?
- Or do I need to rethink my whole deployment workflow?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 zc.buildout 和 djangorecipe - 正如 Jacob Kaplan-Moss 所说,
http://jacobian.org/writing/django-apps-with-buildout/
除了一般的“文明”之外,djangorecipe 还允许你拥有多个设置文件,这些设置文件可以互相导入。然后你可以指定在特定安装中使用哪个设置文件
development.py
live.py
...这只是冰山一角。
Have a look at zc.buildout and djangorecipe - which, as Jacob Kaplan-Moss puts it,
http://jacobian.org/writing/django-apps-with-buildout/
Apart from general 'civilizedness", djangorecipe allows you to have more than one settings file, which can import from each other. Then you can specify which settings file to use in a particular installation.
development.py
live.py
... and that's only the tip of the iceberg
我想问为什么你的现场和开发站点有不同的项目。为什么不将所有差异保留在设置/配置上(例如,就像使用
PROJECT_NAME
所做的那样),但保持项目的通用性?看起来,当你让每个站点变得不同时,开发和生活之间出现错误的机会就会增加。除此之外,我认为你所做的或多或少都还不错。我见过的另一种模式是这样的:
I would question why you have different projects for your live and dev sites. Why not keep all differences down to settings/configuration (as you do with
PROJECT_NAME
, for example) but keep your projects common? It seems you only increase any chances for error between dev and live the more you make each site diffferent.Other than that, I think what you're doing is more or less fine. The other pattern I've seen is something like: