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

发布于 2024-08-22 21:30:35 字数 164 浏览 6 评论 0原文

关于Django Sites模块和manage.pysyncdb

Auth模块可以在.\manage.pysyncdb期间提示询问管理站点的默认超级用户。我希望看到默认站点域名也发生类似的情况。目前它是 example.com,硬编码的,除非我使用管理网站来更改它。我想在syncdb期间更改它。

Regarding Django Sites module and 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-08-29 21:30:35

我制作了一个可以插入并运行的小型 django 应用程序。插入它:

  1. 将其下载到项目目录或您的项目可以找到的位置。
  2. 在 settings.py INSTALLED_APPS 中,在其所依赖的末尾或“django.contrib.sites”之后添加“site_default”(应用程序名称)。
  3. 运行manage.pysyncdb
    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: [email protected]
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

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

  1. download it into project directory or into where your project can find.
  2. add, in your settings.py INSTALLED_APPS, "site_default" (the app name) at the end or after "django.contrib.sites" that it depends on.
  3. Run manage.py syncdb
    or 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.

Source code:
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: [email protected]
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
两仪 2024-08-29 21:30:35

您可以自己执行此操作:

  1. 创建一个管理命令来提示您的新站点,
  2. 将其连接到 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< /code> 就完成了。我确实想知道为什么 Django 中还没有这个,我讨厌 example.com。

将所有这些放入一个小应用程序中,并在您所做的每个项目中重复使用它。如果您将应用程序发布到 google code 或 django 的 bug tracker 等地方,则会获得奖励积分。

You can do this yourself:

  1. Create a management command to prompt for your new site
  2. connect it to the post_syncdb signal

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:

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文