I am trying to work with Sites
Model of Django.
I dont quite understand why SITE_ID
should be SITE_ID = 1
.
in the docs:
The ID, as an integer, of the current site in the django_site database table. This is used so that application data can hook into specific sites and a single database can manage content for multiple sites.
why 1
? what is the current site
? this is not clearly explained in the docs.
lets say, I have www.somecoolsite.com
and some other subdomains like www.wow.coolsite.com
and www.awesome.coolsite.com
I want to render different content depending on domain name.
my question is, or better, are:
- Do I have to add all those domains into
Sites
Table in DB? - if so, how should I set
SITE_ID
in settings? Do I have to set all ids likeSITE_ID = 1
,SITE_ID = 2
.. etc? - what does
current site
has to do withSITE_ID = 1
?
I am a bit confused here.
I thought, each Site (e.g. www.wow.coolsite.com
) should be a separate django project so that they can have their own settings.py? and in each of those settings.py's, I will set the id of that page from Sites table? but then there are many django projects which also doesnot make sense to me.
This is a late answer but for anyone else having SITE_ID issues and Site problems. Inside the database, django has a django_site table with(id, domain, name). This is where django stores the SITE_IDs. Mine was actually 5 in the database but i had it set to SITE_ID=1 in the settings.
Knowing that, i can now go back to the database and clear it to get back to zero or use the actual id in the database.
Things would be much easier to understand if Django's default SiteAdmin included the id field in the
list_display
fields.To do this, you can redefine SiteAdmin (anywhere in your app, but I'd recommend your admin.py or maybe your urls.py) like this:
After including this code snippet, the ID for each "Site" will be shown in the first column of the admin list and inside the form as a read only field. These 'id' fields are what you need to use as SITE_ID:
The concept is that each different site runs in a different application server instance, launched using a different yourdomain_settings.py that then includes a base_settings.py with the rest of the common configuration.
Each of these yourdomain_settings.py will define its own SITE_ID and all other different settings.py parameters that they need to look and be different from each other (static resources, templates, etc.) then you'll define a DJANGO_SETTINGS_MODULE environment variable pointing to that specific yourdomain_settings.py file when launching the application server instance for that domain.
A further note:
get_current_site(request)
does needrequest
to be available for it to work. If your code doesn't have one, you can useSite.objects.get_current()
that however will need a SITE_ID properly defined in the running application server's settings.Django was created from a set of scripts developed at a newspaper to publish content on multiple domains; using one single content base.
This is where the "sites" module comes in. Its purpose is to mark content to be displayed for different domains.
In previous versions of django, the
startproject
script automatically added thedjango.contrib.sites
application toINSTALLED_APPS
, and when you didsyncdb
, a default site with the URLexample.com
was added to your database, and since this was the first site, its ID was1
and that's where the setting comes from.Keep in mind that starting from 1.6, this framework is not enabled by default. So if you need it, you must enable it
The
SITE_ID
setting sets the default site for your project. So, if you don't specify a site, this is the one it will use.So to configure your application for different domains:
example.com
to whatever your default domain is. You can do this from the django shell, or from the admin.site = models.ForeignKey(Site)
on_site = CurrentSiteManager()
Now, when you want to filter content for the default site, or a particular site:
The documentation has a full set of examples.
This is covered in the documentation for the Sites framework:
But if you didn't want to do it that way, you can not set
SITE_ID
at all and just look up the current site based on the domain name in your views usingget_current_site
:This link explains it:
Also, if you wanna figure out how to modify models and set the views You may take a look at this link: https://django.cowhite.com/blog/managing-multiple-websites-with-a-common-database-in-django-the-sites-framework/