Answer the question
In order to leave comments, you need to log in
Is it possible to use one Django 2 configuration for multiple domains at once (multi-domains)?
Good time everyone.
Since some time, I began to make sites only on Django.
I host them on a VPS.
I buy a domain, I attach it. I upload the project via GIT.
Further PostgreSQL, Gunicorn, Supervisor, Nginx for statics, Certificate https.
If necessary, then Celery + RabbitMQ
So, if this is a complex project where, in principle, a lot of things are used, then I consider it justified to buy a whole server for this business.
site1, it has its own templates, views and, if necessary, even models. This is for one client.
site2, it has its other templates, views, and again, if necessary, also models. This is for the second client.
site3 for the third one
...
etc. how much the server will basically pull. And now the question is:
But very often there are many projects where it’s just a simple corporate website or a landing page or some kind of showcase. In general, sites where there is no particular load. And there is a strong desire to set one Django configuration, but use it for different projects. Well, I'm making applications:
Is it possible to use one Django 2 configuration for several domains at once (multi-domains)?
I can't find information anywhere on how to do this. And so that it all worked for Django 2, which worked on VPS. Kindly, throw some understandable manual.
Sincerely. Maxim
Answer the question
In order to leave comments, you need to log in
You can, with the usual nginx setup (wildcard, for example). And in the application you already process it as you want.
server_name *.mydomain.com;
Recently, I also encountered this problem, only the task was to host several identical stores on one Django application.
settings.py:
INSTALLED_APPS = [
.....
'django.contrib.sites',
]
MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
'app_name.middleware.multisite.DomainRouterMiddleware',
..............
]
class Site(models.Model):
"""
Модель в которой будут храниться подключаемые сайты
"""
domain = models.CharField("Домен", max_length=120)
name = models.CharField("Имя", max_length=320)
appname = models.CharField("Имя приложения", validators=[_appname_validator], max_length=120)
objects = SiteManager()
def __str__(self):
return self.name
class Meta:
db_table = "site"
verbose_name = "Сайт"
verbose_name_plural = "Сайты"
ordering = ('domain',)
class DomainRouterMiddleware(object):
"""
Middleware в которой происходит редирект на нужный urlconf.
Пример: Есть объект < 'Site' domain="example.com", name='Example', appname='example'>
При запросе на сайт example.com пользователь будет перенаправлен в приложение example в модуль example.urls
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
host = request.get_host() # Получаем имя доменна в запросе
site = Site.objects.get(domain=host) # Пытаемся найти данный домен в django sites
request.urlconf = '%s.urls' % site.appname # Перенаправляем пользователя в нужный url конфиг
return self.get_response(request)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question