Ticket #7358: 003_create_default_site.patch

File 003_create_default_site.patch, 1.3 KB (added by Etienne Robillard, 16 years ago)

Uses socket.gethostname to resolve the default domain name

  • django/contrib/sites/management.py

    old new  
    66from django.db.models import signals
    77from django.contrib.sites.models import Site
    88from django.contrib.sites import models as site_app
     9import socket
    910
    10 def create_default_site(app, created_models, verbosity):
     11def get_default_hostname():
     12    try:
     13        hostname = socket.gethostname()
     14    except socket.error:
     15        from django.conf import settings
     16        hostname = getattr(settings.DEFAULT_SITE_NAME, 'localhost')
     17    return hostname
     18
     19def create_default_site(app, created_models, verbosity=1):
    1120    if Site in created_models:
    12         if verbosity >= 2:
    13             print "Creating example.com Site object"
    14         s = Site(domain="example.com", name="example.com")
     21        # Don't let script-kiddies ruin your efforts by
     22        # hardcoding something as stupid as example.com here.
     23        hostname = get_default_hostname()
     24        if verbosity >= 1:
     25            print "Creating default site: <%s>" % repr(hostname)
     26        s = Site(domain=hostname, name=hostname)
    1527        s.save()
    1628
    1729dispatcher.connect(create_default_site, sender=site_app, signal=signals.post_syncdb)
Back to Top