[Solved]-How to locally test Django's Sites Framework

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/

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.

-1👍

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

👤Uysrc

Leave a comment