Django register a new site in shell
To register a new site in Django using the shell, you can follow these steps:
-
Open a terminal or command prompt and navigate to your Django project directory.
-
Run the following command to start the Django shell:
python manage.py shell
This will open a Python interpreter with the Django project's settings loaded.
-
In the shell, import the
sites
module:from django.contrib.sites.models import Site
-
Create a new site object:
site = Site(domain='example.com', name='Example')
Replace
example.com
with the domain name of your new site. -
Save the site object:
site.save()
This will create a new site in the
django_site
table in your database. -
Verify that the site has been created by running:
Site.objects.all()
This should return a list of all sites in your database, including the new site you just created.
That's it! Your new site is now registered in Django.
Note: If you want to register a site with a specific ID, you can specify the id
parameter when creating the site object:
site = Site(id=1, domain='example.com', name='Example')
Make sure to adjust the id
value to a unique integer that doesn't conflict with existing sites in your database.