关于Django Sites模块和manage.py syncdb
在。\ manage.py syncdb期间,Auth模块可以提示要求管理站点的默认超级用户。我想看到默认站点域名发生类似的事情。目前它是example.com,硬编码除非我使用管理员网站进行更改。我想在syncdb期间更改它。
答案 0 :(得分:6)
我制作了一个可以插入和播放的小型django应用程序。插上电源:
manage.py syncdb
或manage.py createdefaultsite
屏幕截图:
(pinax-dev)>manage.py createdefaultsite
Site domain name: mydomain.com
Site display name: My Site!
(pinax-dev)
它带有单元测试。 要运行单元测试:
(pinax-dev)>manage.py test site_default
“site_default”是应用名称。
源代码: http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz
更多截图:
(pinax-dev)> manage.py syncdb
Creating table...
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Username: administrator
E-mail address: who@who.com
Password:
Password (again):
Superuser created successfully.
Would you like to change the default site domain name? (yes/no)[default:no]: yes
Site domain name: mydomain.com
Site display name: My Site!
...
Installing index for signup_codes.SignupCode model
Installing index for signup_codes.SignupCodeResult model
答案 1 :(得分:4)
你可以自己做:
post_syncdb
信号该命令可让您从命令行方便地设置站点。将其连接到信号意味着每当安装sites
应用程序时都会提示您。例如:
from django.contrib.sites import models as sites_app
signals.post_syncdb.connect(create_site, sender=sites_app)
在编写create_site
函数(信号处理程序)时,您几乎可以完全复制auth
模块的方法:
def create_site(app, created_models, verbosity, **kwargs):
from django.contrib.sites.models import Site
from django.core.management import call_command
if Site in created_models and kwargs.get('interactive', True):
msg = "\nYou just installed Django's sites system, which means you don't have " \
"any sites defined.\nWould you like to create one now? (yes/no): "
confirm = raw_input(msg)
while 1:
if confirm not in ('yes', 'no'):
confirm = raw_input('Please enter either "yes" or "no": ')
continue
if confirm == 'yes':
call_command("createsite", interactive=True)
break
现在您只需创建管理命令createsite
即可。我确实想知道为什么这不是已经在Django中了,我讨厌example.com。
将所有这些放入一个小应用程序中,并为每个项目重复使用它。如果你在谷歌代码或django的bug追踪器之类的地方发布应用程序,奖励积分。