[Django]-Automatically prompt to update default site domain name when running Django’s ./manage.py syncdb?

6👍

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: 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
👤peiwei

4👍

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.

Leave a comment