35👍
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.
19👍
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 🙂
- [Django]-Django static files versioning
- [Django]-Django: How to access original (unmodified) instance in post_save signal
- [Django]-Django – How to access the verbose_name of a Model in its Admin Module?
- [Django]-How can I test https connections with Django as easily as I can non-https connections using 'runserver'?
- [Django]-Additional field while serializing django rest framework
- [Django]-Django rest framework – how do you flatten nested data?
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
- [Django]-Virtualenv and source version control
- [Django]-How to generate urls in django
- [Django]-Django is "unable to open database file"
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‘.
- [Django]-Convert string array to array in javascript
- [Django]-How can I check database connection to mysql in django
- [Django]-Docker-compose with django could not translate host name "db" to address: Name or service not known