Creation of 2 django sites that contain 90% of the data and code

I have two closely related sites, the main site and the mobile site hosted as a django application. They will have the same functionality and must access the same data. The main difference is that the templates will be different, and the way you structure your site will be different.

I have two separate virtual hosts, one for each (although I do not need this). My first thought was that the Django site infrastructure helps solve this problem, but the docs don't seem to describe my use case.

Can someone tell me if I am right? The urls.py URL must be different, because, for example, the home page is completely different from the applications. The main goal is for data in two different applications to be shared and for code that does not need to be duplicated.

From the main site:

  • The user submits an item that is stored in the model.

From a mobile site:

  • The user browses the list of elements and sees the just entered on the main site
  • User gives 5 stars rating for recently added item.

From the main site:

  • The user views a list of highly rated items, and a recently added item (which now has a high rating) is displayed in the list.
+4
source share
3 answers

OK, both answers are great and contributed to what I chose for my final decision.

There is an option ROOT_URLCONF in the settings.py file. I created two settings.py files called settings_desktop.py and settings_mobile.py, and the following code was used in each of them:

from settings.py import * ROOT_URLCONF = 'myapp.urls_mobile' 

(or in the case of the desktop, myapp.urls_desktop)

In fact, it provides many interesting features, such as the ability to use different template directories for each site, although in fact I will not.

Then I created two versions of the wsgi file, where the only difference was in this line:

 os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings_mobile' 

or

 os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings_desktop' 

In each of the virtual hosts, the only difference is the WSGIScriptAlias ​​line, which points to a different wsgi file for each host.

This allowed me to efficiently use one django application, which could easily host both sites.

Thank you for helping to come up with a good solution.

+1
source

Take a look at this answer to a similar question . Basically, you can use the same views and just return different templates based on the user agent. In addition, if you structure your application logic so that it breaks down into different “applications” in terms of django, you can reuse them if you need different threads with similar components. Hope this makes you run.

UPDATE:

So, let's say you have your main site http://www.mainsite.com/ , which has its own urls.py models.py and views.py, which makes your functions for the main site. Then you are http://www.m.mainsite.com/ , which has its own set of URLs and views. Then you can simply import the main site models and use them in your mobile site views.

+4
source

I did something very similar once. My way to solve this problem with multiple urls.py was something like this:

Create two urlconfs, one for each site;

Create a new middleware:

 from django.utils.cache import patch_vary_headers class DomainMiddleware: def __init__(self): pass def process_request(self, request): #avoid problems when reaching the server directly trough IP host = request.META.get('HTTP_HOST', None) if host is None: return host = host.split(':')[0] #remove port number if host is mobile: urlconf = "mobile.urls" else: urlconf = "default.urls" request.urlconf = urlconf def process_response(self, request, response): patch_vary_headers(response, ('Host',)) return response 

Check also why you should do patch_vary_headers in the docs .

0
source

Source: https://habr.com/ru/post/1334563/


All Articles