Django register a new site
In Django, a "site" is a way to identify a specific domain or URL that your Django project is associated with. When you register a new site in Django, you're telling Django that this site is part of your project and should be included in the project's configuration.
Here's how to register a new site in Django:
Method 1: Using the sites
app
- Make sure you have the
sites
app installed in your Django project. If you don't have it, you can add it to yourINSTALLED_APPS
setting in yoursettings.py
file. - Create a new file in your project's
sites
directory (e.g.,mysite.com
). - In this file, add the following code:
from django.contrib.sites.models import Site
site = Site(domain='mysite.com', name='My Site') site.save()
Replace `mysite.com` with the domain name you want to register.
**Method 2: Using the Django admin interface**
1. Go to the Django admin interface at `http://localhost:8000/admin/` (or the URL you specified in your `settings.py` file).
2. Log in with your admin credentials.
3. Click on "Sites" in the left-hand menu.
4. Click the "Add site" button.
5. Fill in the required information:
* `Domain`: the domain name you want to register (e.g., `mysite.com`).
* `Name`: a human-readable name for the site (e.g., "My Site").
6. Click the "Save" button.
**Verify the site registration**
After registering the site, you can verify that it's been added to your Django project by checking the `sites` table in your database. You can do this using the Django shell or a database client like `sqlite3`.
For example, in the Django shell:
```python
from django.contrib.sites.models import Site
sites = Site.objects.all()
print(sites) # Should print a list of sites, including the new one
That's it! Your new site is now registered in your Django project.