1

I have a Django app that will serve diferent websites.

  • Each one will have it´s own domain
  • Each one will have it´s own sub app with templates and views
  • They all share the same backend, models and data

My aproach

As I already have the database with the segmentation info I need to show the desired products in each site and I will have different views for each sub app, I don´t need to add another field in the models.

I think that it would be easier just to detect the request domain in my main app urls.py and rout home url to the desired sub app.

Something like:

# urls.py

if "name1" in request.domain:
    urlpatterns += [path('', include('app1.urls'))]
if "name2" in request.domain:
    urlpatterns += [path('', include('app2.urls'))]
else:
    urlpatterns += [path('', include('app3.urls'))]

Maybe I should make a middleware and set a global variable I can access in urls.py? Can I use this kind of if statements in urls.py?

Django sites

I checked the Django Sites Framework, but if I understand ir well, it´s more focused in segmenting the database in the models, not the templates and views. In any case I don´t really catch how the Sites framework detects the incomming URL and roots the request to each sub app.

Other intents

I searched for more info and this article can brief the different articles about the issue I found.

https://medium.com/crowdbotics/how-to-use-dynamic-subdomains-in-django-dc1cb2cac00b

But still I don´t get how can I achieve what I need. Sounds very logical just to use my aproach and don´t mess with my database. If it´s possible.

Any clues welcome. Thanks in advance!

Francisco Ghelfi
  • 872
  • 1
  • 11
  • 34
  • I think that won't work because the urls.py won't run at every request. It's initialized one time. What about doing it as a "proxy-view"? – Frank Sep 29 '19 at 13:17
  • Hi @Frank can you extend the idea please? Or point me out some source of info? This is what you mean for example? https://github.com/mjumbewu/django-proxy – Francisco Ghelfi Sep 29 '19 at 14:02
  • No i mean that you create a view that only analyses what the request.domain is and calls another view that does the output. – Frank Sep 29 '19 at 14:09
  • Ok, I get you. I´ll try it. Would you like to post it as an answer? – Francisco Ghelfi Sep 29 '19 at 14:11
  • Did you have already seen that?: https://django-subdomains.readthedocs.io/en/latest/ – Frank Sep 29 '19 at 14:13
  • Saw similar things but it refers to subdomains. I need to operate with different domains, not subdomains. – Francisco Ghelfi Sep 29 '19 at 14:25

0 Answers0