How to use Django's sites framework to manage multiple sites with a single codebase

Django is a powerful web framework that allows developers to create complex web applications with minimal effort. One of the most powerful features of Django is its sites framework, which allows developers to manage multiple sites with a single codebase. In this tutorial, we will show you how to use Django's sites framework to manage multiple sites with a single codebase.

Install Django

The first step is to install Django. You can do this by using the pip command:

pip install django

Once Django is installed, you can create a new project by running the following command:

django-admin startproject myproject

Add the sites framework

Once the project is created, you need to add the sites framework to it. To do this, open the settings.py file in the project directory and add the following line:

INSTALLED_APPS = [ ... 'django.contrib.sites',]

Configure the sites framework

Next, you need to configure the sites framework. To do this, open the settings.py file and add the following lines:

SITE_ID = 1SITE_NAME = 'My Site'SITE_DOMAIN = 'example.com'

The SITE_ID is a unique identifier for the site. The SITE_NAME is the name of the site, and the SITE_DOMAIN is the domain name of the site.

Create the sites

Once the sites framework is configured, you can create the sites. To do this, open the urls.py file in the project directory and add the following lines:

from django.contrib.sites.models import Sitesite1 = Site.objects.create(name='Site 1', domain='site1.example.com')site2 = Site.objects.create(name='Site 2', domain='site2.example.com')

This will create two sites with the specified names and domains.

Configure the sites

Once the sites are created, you need to configure them. To do this, open the settings.py file and add the following lines:

SITE_ID = site1.idSITE_NAME = site1.nameSITE_DOMAIN = site1.domain

This will configure the first site with the specified settings.

Create the views

Once the sites are configured, you need to create the views. To do this, open the views.py file in the project directory and add the following lines:

from django.shortcuts import renderdef index(request): return render(request, 'index.html')

This will create a view that will render the index.html template.

Create the URLs

Once the views are created, you need to create the URLs. To do this, open the urls.py file in the project directory and add the following lines:

from django.urls import pathfrom .views import indexurlpatterns = [ path('', index, name='index'),]

This will create a URL pattern that will map the index view to the root URL.

Deploy the sites

Once the URLs are created, you can deploy the sites. To do this, you can use a web server such as Apache or Nginx. Once the web server is configured, you can deploy the sites by running the following command:

python manage.py runserver

This will start the web server and deploy the sites.

Test the sites

Once the sites are deployed, you can test them by visiting the URLs in a web browser. If everything is working correctly, you should see the index.html template rendered in the browser.

Conclusion

In this tutorial, we have shown you how to use Django's sites framework to manage multiple sites with a single codebase. We have shown you how to install Django, create a project, add the sites framework, configure the sites framework, create the sites, configure the sites, create the views, create the URLs, deploy the sites, and test the sites. We hope you have found this tutorial helpful.

Useful Links