Creating 2 django sites that share 90% data and code
I have two closely related sites, a main site and a mobile site, hosted as a django app. They'll have a lot of the same functionality and need to access the same data. The main difference is the templates will be different and the way the site is structured will be different.
I have two separate virtual hosts, one for each (开发者_如何转开发though I don't have to do it that way). My first thought was that the Django sites framework helps to solve this, but the docs don't seem to describe my use case.
Can someone give me a hint to know if I'm on the right track? The urls.py will need to be different since, for example, the homepage is completely different between the apps. The main goal is that for the data in the two different apps to be shared and the code to manage that does not need to be duplicated.
From the main site:
- User submits an item that is stored in the model
From the mobile site:
- User views a list of items and see the one just entered on the main site
- User gives a 5 star rating on the recently added item
From the main site:
- User views a list of highly rated items and the recently added item (which now has a high rating) shows up on the list.
Have a look at this answer to a similar question. Basically you can just use the same views and just return different templates based on the user-agent. Also, if you structure your application logic so that it is broken up into different "apps" in django terms, then you can re-use them if you need different flows with similar components. Hopefully this gets you off and running.
UPDATE:
So lets say you have your main site http://www.mainsite.com/ which has it's own urls.py models.py and views.py that makes your functionality for the main site. Then you have http://www.m.mainsite.com/ which has it's own set of urls, and views. Then you can just import the main site's models and use them in the mobile sites views.
OK, both answers are great and contributed to what I chose for my final solution.
In the settings.py file there is an option called ROOT_URLCONF. I created two settings.py files, called settings_desktop.py and settings_mobile.py and in each of these used the following code:
from settings.py import *
ROOT_URLCONF = 'myapp.urls_mobile'
(or in the case of the desktop, myapp.urls_desktop)
This actually gives a lot of cool features such as being able to use different template directories for each site, though really I'm not going to do that.
Then I created two versions of the wsgi file where the only difference was 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 that points to the different wsgi file for each host.
This allowed me to effectively use one django app that could easily accommodate both sites.
Thanks for helping work out a good solution to this.
I did something very similar once. My way of solving this problem of multiple urls.py was something like this:
Create two urlconf, 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 have to do the patch_vary_headers on the docs.
精彩评论