运行Django的./manage.py syncdb时自动提示更新默认站点域名?

时间:2020-12-12 18:04:05

Regarding Django Sites module and manage.py syncdb

关于Django Sites模块和manage.py syncdb

The Auth module can prompt to ask for default superuser for the admin site, during .\manage.py syncdb. I would like to see similar things happen for the default site domain name. Currently it is example.com, hardcoded unless I use admin web site to change it. I want to change it during syncdb.

在。\ manage.py syncdb期间,Auth模块可以提示要求管理站点的默认超级用户。我想看到默认站点域名发生类似的事情。目前它是example.com,硬编码,除非我使用管理员网站进行更改。我想在syncdb期间更改它。

2 个解决方案

#1


6  

I made a small django app that can be plugged in and play. To plug it in:

我制作了一个可以插入和播放的小型django应用程序。将其插入:

  1. download it into project directory or into where your project can find.
  2. 将其下载到项目目录或项目可以找到的位置。
  3. add, in your settings.py INSTALLED_APPS, "site_default" (the app name) at the end or after "django.contrib.sites" that it depends on.
  4. 在您的settings.py INSTALLED_APPS中添加“site_default”(应用程序名称),或者在它依赖的“django.contrib.sites”之后添加。
  5. Run manage.py syncdb or manage.py createdefaultsite
  6. 运行manage.py syncdb或manage.py createdefaultsite

Screen shot:

截屏:

(pinax-dev)>manage.py createdefaultsite
Site domain name: mydomain.com
Site display name: My Site!
(pinax-dev)

It comes with a unit test. To run unit test:

它带有单元测试。要运行单元测试:

(pinax-dev)>manage.py test site_default

"site_default" is the app name.

“site_default”是应用名称。

Source code: http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz

源代码:http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz

More Screenshot:

更多截图:

(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

#2


4  

You can do this yourself:

你可以自己做:

  1. Create a management command to prompt for your new site
  2. 创建管理命令以提示您的新站点
  3. connect it to the post_syncdb signal
  4. 将它连接到post_syncdb信号

The command will let you set the site conveniently from the command line. Connecting it to the signal will mean you get prompted whenever the sites app is installed. eg:

该命令将允许您从命令行方便地设置站点。将其连接到信号意味着每当安装站点应用程序时都会提示您。例如:

from django.contrib.sites import models as sites_app
signals.post_syncdb.connect(create_site, sender=sites_app)

When writing the create_site function (signal handler), you can copy the auth module's approach almost exactly:

在编写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

Now you just need to create your management command createsite and you're done. I do wonder why this isn't already in Django though, I hate example.com.

现在你只需要创建你的管理命令creatite就可以了。我确实想知道为什么这不是已经在Django中了,我讨厌example.com。

Put all this into a little app and reuse it for every project your do. Bonus points if you post the app somewhere like google code or django's bug tracker.

将所有这些放入一个小应用程序中,并为每个项目重复使用它。如果你在谷歌代码或django的bug追踪器之类的地方发布应用,奖励积分。

#1


6  

I made a small django app that can be plugged in and play. To plug it in:

我制作了一个可以插入和播放的小型django应用程序。将其插入:

  1. download it into project directory or into where your project can find.
  2. 将其下载到项目目录或项目可以找到的位置。
  3. add, in your settings.py INSTALLED_APPS, "site_default" (the app name) at the end or after "django.contrib.sites" that it depends on.
  4. 在您的settings.py INSTALLED_APPS中添加“site_default”(应用程序名称),或者在它依赖的“django.contrib.sites”之后添加。
  5. Run manage.py syncdb or manage.py createdefaultsite
  6. 运行manage.py syncdb或manage.py createdefaultsite

Screen shot:

截屏:

(pinax-dev)>manage.py createdefaultsite
Site domain name: mydomain.com
Site display name: My Site!
(pinax-dev)

It comes with a unit test. To run unit test:

它带有单元测试。要运行单元测试:

(pinax-dev)>manage.py test site_default

"site_default" is the app name.

“site_default”是应用名称。

Source code: http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz

源代码:http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz

More Screenshot:

更多截图:

(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

#2


4  

You can do this yourself:

你可以自己做:

  1. Create a management command to prompt for your new site
  2. 创建管理命令以提示您的新站点
  3. connect it to the post_syncdb signal
  4. 将它连接到post_syncdb信号

The command will let you set the site conveniently from the command line. Connecting it to the signal will mean you get prompted whenever the sites app is installed. eg:

该命令将允许您从命令行方便地设置站点。将其连接到信号意味着每当安装站点应用程序时都会提示您。例如:

from django.contrib.sites import models as sites_app
signals.post_syncdb.connect(create_site, sender=sites_app)

When writing the create_site function (signal handler), you can copy the auth module's approach almost exactly:

在编写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

Now you just need to create your management command createsite and you're done. I do wonder why this isn't already in Django though, I hate example.com.

现在你只需要创建你的管理命令creatite就可以了。我确实想知道为什么这不是已经在Django中了,我讨厌example.com。

Put all this into a little app and reuse it for every project your do. Bonus points if you post the app somewhere like google code or django's bug tracker.

将所有这些放入一个小应用程序中,并为每个项目重复使用它。如果你在谷歌代码或django的bug追踪器之类的地方发布应用,奖励积分。