5

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

matyas
  • 2,696
  • 23
  • 29

2 Answers2

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.

YPCrumble
  • 26,610
  • 23
  • 107
  • 172
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" ?

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • yes I just realized when I posted the question thank you! – matyas Jan 30 '17 at 11:36
  • @matyas this doesn't answer the question - was this possible and if so what did you need to do to get it to work? – YPCrumble Sep 21 '17 at 03:49
  • @YPCrumble 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 shortcut](http://example.com)https://docs.djangoproject.com/en/1.11/ref/contrib/sites/#django.contrib.sites.shortcuts.get_current_site .this request has to be passed to the get_current_site function get_current_site(request) and this will give you the current site – matyas Sep 21 '17 at 09:14