How to locally test Django's Sites Framework
Asked Answered
P

3

12

Django has the sites framework to support multiple web site hosting from a single Django installation.

EDIT (below is an incorrect assumption of the system)


I understand that middleware sets the settings.SITE_ID value based on a lookup/cache of the request domain.


ENDEDIT

But when testing locally, I'm at http://127.0.0.1:8000/, not http://my-actual-domain.com/

How do I locally view my different sites during development?

Pycno answered 8/2, 2010 at 17:58 Comment(1)
The link expired now. Please fix it.Surcingle
C
19

Create a separate settings.py file for every site, including an appropriate SITE_ID setting. Of course you can use the import statement to share common setting between files.

From now on, when running Django development server specify the --settings option to tell Django which site to run.

For example (assuming you've got two setting files - settings_first.py and settings_second.py):

manage.py runserver --settings settings_first

will run the first site, and

manage.py runserver --settings settings_second

will give you an access to the second site.

You can also run them simultaneously, specifying different ports:

manage.py runserver 8001 --settings settings_first

manage.py runserver 8002 --settings settings_second

The above commands (run on two different consoles) will make the first website accesible under http://127.0.0.1:8001/, and the second one under http://127.0.0.1:8002/

Chufa answered 8/2, 2010 at 19:55 Comment(0)
Z
4

Maybe you are mislead by the documentation. You wrote:

I understand that middleware sets the settings.SITE_ID value based on a lookup/cache of the request domain.

This is not the case. It works exactly the other way around. Django uses the settings.SITE_ID value to look up the correct Site object in the database. This returns your prefered domain and site name.

The sites application was designed to fill the (in my opinion) rare usecase that you want to have multiple sites with the same database in the background. This allows you to publish the same articles on different sites but still have the flexibility that some models are available just for a single site.

For developing multiple projects (that doesn't actually make use of the sites framework) you don't have to specify anything special. You can use the default SITE_ID set to 1. For utilizing the admin's view on website links you can set in your development database the Site's domain to localhost:8000.

If you want to develop multiple sites using the same database (and make use of the sites framework) you must have each project with a distinct SITE_ID but the same database settings. The values for SITE_ID in each project on your development machine are in most cases the same as for your production servers.

Zadoc answered 8/2, 2010 at 23:0 Comment(1)
Thanks Gregor. I suspect I am greatly mislead. I will likely create middleware to do what I though Django was already doing, and have it look for a query string attribute 'site=my-site-1.com' when the requested domain is in settings.DEVELOPMENT_SERVERS, like: if request.META['SERVER_NAME'] in settings.DEVELOPMENT_SERVERS: #do lookup and set the site in the sessionPycno
O
-1

FYI - I released django-dynamicsites today which has facilities to solve this issue - https://bitbucket.org/uysrc/django-dynamicsites/src

Oospore answered 24/3, 2011 at 16:35 Comment(1)
link appears to be brokenChadwell

© 2022 - 2024 — McMap. All rights reserved.