Multiple Domains django app on heroku - django

Django have nice sites framework, so i thought can it posissible to store on heroku app with multi-settings structure and join it with differents domains on one heroku instance?

According to the docs, you can use Django sites framework with Heroku if you set SITE_ID = None. In that case Django will automatically detect current site based on request.get_host() method. So you only have to add specific hosts in admin panel.
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.
https://docs.djangoproject.com/en/1.8/ref/contrib/sites/

Related

How do I change my weblate site url

I installed my own weblate server somewhere, then I switched my nginx configuration to a domain name. I went into weblate's django admin, in sites section and updated the site entry to point to the right location.
I couldn't find anything related to this within settings.py. There's a default site id, but it's already set to 1.
After doing this, urls to projects were still using old url.
Update site entry in django admin and Restart the weblate django application. After restarting weblate, the new url from database started working.
There is also this documented section:
https://docs.weblate.org/en/latest/admin/management.html#changesite
Which should refresh website.

Django powered site and micro sites – do I have to create a separate project for each microsite?

I'm looking to build a Django website with the main site on example.com. On this domain, a user can register using an email and password and start writing blog posts at example.com/blog. Admins will be able to login into the Django Admin to alter posts and change content on the other pages of example.com.
The website will have one microsite each year on subdomains, e.g., 2013.example.com, 2014.example.com, etc. Admins will be able to edit content using the Django Admin.
I'm going to deploy the Django project onto Heroku.
The question that I would answered is whether I should create a new Django app and Heroku deployment for each microsite and the main site? Or, whether I can create it all as one Django project?
The latter seems to make more sense to me, but unsure how I would go about it.

Is it possible to override the database settings in django for the admin

Is it possible to override the database settings for the admin part? (using django admin)
I want to use a readonly database user for my website but be able to use a read/write user for the admin part, without having to deal with two settings files and two different wsgi processes.
There's no way to do this with a single instance. However, you can run two instances of your application. On one, you ferry only the requests for the main site and the other only those for the admin. Or you could serve the admin up on a subdomain of your site (all this would be done with your reverse proxy server: nginx, Apache, etc.)
Since you're running two instances, you can customize the settings for each, so you can then also customize the database user being utilized.

Use Django flatpages without sites

Is there any that I can have a catch all site with flatpage framework in Django?
I have one site but you can get to it through a few DNS names that change constantly. I need to map these all to a single site and flatpages seems bent on me pre-specifying my domain entries.
You need to configure your webserver correctly so that the requests from all domains get forwarded to your only django instance! You cannot run flatpages without having django.contrib.sites in your INSTALLED_APPS, but that is no problem for your case, the actual site will always be determined with the SITE_ID defined in your settings.py. The sites framework does not check the request to check which is the actual site. If you run multiple sites, you have to run multiple django instances that use different settings, which define different SITE_IDs!
So just check your webserver to have everything directed to your django instance!

How to locally test Django's Sites Framework

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?
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/
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.
FYI - I released django-dynamicsites today which has facilities to solve this issue - https://bitbucket.org/uysrc/django-dynamicsites/src