问题描述
我正在一个Django项目中工作,我需要访问管理区域进行本地开发,但希望在部署的站点中禁用它(出于安全原因等等)。
I am currently working in a Django project for which I need access to the admin area for local development, but want to disable it in the deployed site (for security reasons, among others).
如何以编程方式( ie 使用 settings.py
)。
How can I achieve this programmatically (ie using settings.py
).
非常感谢。
推荐答案
首先,建立一个方案,使您的生产服务器可以有不同于开发服务器。一个简单的方法是使用源代码控制忽略的local_settings.py文件,但是有很多优秀的方法可以实现。
First, establish a scheme so that your production server can have different settings than your development servers. A simple way to do that is with a source-control-ignored local_settings.py file, but there are many fancier ways to do it.
然后,在你的设置中。 py文件,put:
Then, in your settings.py file, put:
ADMIN_ENABLED = True
并在您的仅生产设置文件中,放置:
and in your production-only settings file, put:
ADMIN_ENABLED = False
然后在您的urls.py:
Then in your urls.py:
if settings.ADMIN_ENABLED:
urlpatterns += patterns('',
(r'^admin/(.*)', include(admin.site.urls)),
# ..maybe other stuff you want to be dev-only, etc...
)
这篇关于如何在部署的项目中禁用Django的管理员,但要保留本地开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!