Is it possible to use the Django Sites Framework with multiple Sites on the same instance?
Asked Answered
L

2

6

I have defined multiple sites as the documentation of the Site Framework suggested.

I understand that if I would run mulitple instances of my application with each of them having a different settings file (different SITE_ID), Django would always know which Site to use.

What I was trying to do is to run a single instance, where multiple sites are available, and the right Site should be chosen depending on the current url of the site.

The Sites documentation states:

The SITE_ID setting specifies the database ID of the Site object associated with that particular settings file. If the setting is omitted, the get_current_site() function will try to get the current site by comparing the domain with the host name from the request.get_host() method.

So I tried to remove the SITE_ID from my settings.py and was hoping that Django would check the domain to find the current Site as stated above, howewer this fails with the following exception:

You're using the Django "sites framework" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting or pass a request to Site.objects.get_current() to fix this error.

So it seems like although the documentation suggests otherwise, this setting is not ommitable

I understand that using the Sites Framework like this would lead to problems when there is no Request object available to find the current Site, but this should not be a problem in the context of my application.

Is it possible to use the Sites Framework without hard-coding the SITE_ID in the settings file by just checking the current domain of the application?

I am using Django Version 1.9.9 with Python 3.4.3

Launceston answered 30/1, 2017 at 11:30 Comment(0)
S
1

To "check the current domain" you need to have a request - as clearly mentionned in the error message :

or pass a request to Site.objects.get_current()

Else how would the code know the "current domain" ?

Sale answered 30/1, 2017 at 11:35 Comment(3)
yes I just realized when I posted the question thank you!Launceston
@Launceston this doesn't answer the question - was this possible and if so what did you need to do to get it to work?Foreglimpse
@Foreglimpse yes it was possible. If you do not have a site_id defined in your settings you will need to have a requestcontext in order for the Sites framework to know what the current site is. documentation of the get_current_site shortcutdocs.djangoproject.com/en/1.11/ref/contrib/sites/… .this request has to be passed to the get_current_site function get_current_site(request) and this will give you the current siteLaunceston
F
2

The best solution is to simply add the Sites framework middleware:

'django.contrib.sites.middleware.CurrentSiteMiddleware'

This automatically passes a request object to Site.objects.get_current() on every request.

Foreglimpse answered 21/9, 2017 at 15:39 Comment(0)
S
1

To "check the current domain" you need to have a request - as clearly mentionned in the error message :

or pass a request to Site.objects.get_current()

Else how would the code know the "current domain" ?

Sale answered 30/1, 2017 at 11:35 Comment(3)
yes I just realized when I posted the question thank you!Launceston
@Launceston this doesn't answer the question - was this possible and if so what did you need to do to get it to work?Foreglimpse
@Foreglimpse yes it was possible. If you do not have a site_id defined in your settings you will need to have a requestcontext in order for the Sites framework to know what the current site is. documentation of the get_current_site shortcutdocs.djangoproject.com/en/1.11/ref/contrib/sites/… .this request has to be passed to the get_current_site function get_current_site(request) and this will give you the current siteLaunceston

© 2022 - 2024 — McMap. All rights reserved.