Where do I set the domain for my Django Sites framework site, when I only have one?
Asked Answered
C

5

28

I have a Django project for a simple blog/forum website I’m building.

I’m using the syndication feed framework, which seems to generate the URLs for items in the feed using the domain of the current site from the Sites framework.

I was previously unaware of the Sites framework. My project isn’t going to be used for multiple sites, just one.

What I want to do is set the domain property of the current site. Where in my Django project should I do that? Somewhere in /settings.py?

Claustral answered 5/9, 2012 at 20:20 Comment(1)
Ah: kind of a duplicate of #3536623Claustral
C
36

If I understand correctly, Sites framework data is stored in the database, so if I want to store this permanently, I guess it’s appropriate in an initial_data fixture.

I fired up the Django shell, and did the following:

>>> from django.contrib.sites.models import Site
>>> one = Site.objects.all()[0]
>>> one.domain = 'myveryspecialdomain.com'
>>> one.name = 'My Special Site Name'
>>> one.save()

I then grabbed just this data at the command line:

python manage.py dumpdata sites

And pasted it into my pre-existing initial_data fixture.

Claustral answered 5/9, 2012 at 20:28 Comment(5)
One correction -- Site.domain should just be the domain name, not a URL. E.g. example.com, not http://example.com/Watcher
@PaulBissex: ah, thank you — I’ve updated the code example accordingly.Claustral
I need to restart my apache to make the update works. # /etc/init.d/apache2 restartPainter
Is there no better way to define this in the settings or so?Springy
I get an error messagedjango.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.Springy
P
20

The other answers suggest to manually update the site in the admin, shell, or your DB. That's a bad idea—it should be automatic.

You can create a migration that'll do this automatically when you run your migrations, so you can be assured it's always applied (such as when you deploy to production). This is also recommended in the documentation, but it doesn't list instructions.

First, run ./manage.py makemigrations --empty --name UPDATE_SITE_NAME myapp to create an empty migration. Then add the following code:

from django.db import migrations
from django.conf import settings


def update_site_name(apps, schema_editor):
    SiteModel = apps.get_model('sites', 'Site')
    domain = 'mydomain.com'

    SiteModel.objects.update_or_create(
        pk=settings.SITE_ID,
        defaults={'domain': domain,
                  'name': domain}
    )


class Migration(migrations.Migration):

    dependencies = [
        # Make sure the dependency that was here by default is also included here
        ('sites', '0002_alter_domain_unique'), # Required to reference `sites` in `apps.get_model()`
    ]

    operations = [
        migrations.RunPython(update_site_name),
    ]

Make sure you've set SITE_ID in your settings. Then run ./manage.py migrate to apply the changes :)

Phaih answered 5/3, 2018 at 10:42 Comment(6)
To perform such a migration without having to create a purpose built app see Thom Wiggers answer.Ahmedahmedabad
This answer is nearly perfect. I had to do a bit of work to understand what the above file was. It is a migration file, created by the "makemigrations" command. Edit that file as shown and run migrate etc.Cantankerous
What is " ('sites', '0002_alter_domain_unique')" doing?Crushing
@Crushing sorry I can't remember, this was a long time ago. IIRC it was something created by Django. Someone else here may know what it is.Phaih
What is the correct way to use this migration in multiple environments where each domain name is different? Is it OK that migrations read env vars or ?Agalloch
@Crushing it's just a schema change. Here, it makes sure that the db schema for the sites framework exists. @oglop: if you need to create different site objects you could have some issues with auto-increment id (see code.djangoproject.com/ticket/15573)Greensboro
R
8

You can change it using django admin site.

Just go to 127.0.0.1:8000/admin/sites/

Rubinrubina answered 4/2, 2018 at 22:17 Comment(3)
admin/sites does not existSpringy
@Sören Try with 127.0.0.1:8000/admin/ and search for the sites section. I did this on django 2.0.1, maybe it changed on new versions and is no longer called sites.Rubinrubina
Thanks a lot, this is the only answer that helped among all the answers on the internet.Calc
H
7

For those who are struggling to find "Sites" section on Django's admin page, for newer Django versions you need to enable the optional Sites Framework like so:

On your settings.py file, add this to your "INSTALLED_APPS":

'django.contrib.sites'

Then specify an ID for the default site (since it's probably your first site to be specified, you can use ID 1):

SITE_ID = 1

Run your migrations and check if the "Sites" section is available on your Django's admin page.

More details at https://docs.djangoproject.com/en/3.0/ref/contrib/sites/#enabling-the-sites-framework

Humiliating answered 2/3, 2020 at 12:13 Comment(0)
S
5

You can modify the Site entry in your database manually. Navigate to the table called 'django_site'. Then, you should only see one entry (row). You'll want to modify the field (column) named 'domain'.

Sunglasses answered 6/9, 2012 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.