问题
I made web-app with django which is perfectly looking nice over desktop-browser but in mobile-browser some pages are not looking perfectly.
My template directory is myproject/template/ I want to make new templates for mobile browsers myproject/template_mobile/ and whenever webpages access trough mobile it redirect to myproject/template_mobile/ template else it goes trough myproject/template/. Is there any way to make it form setting.py or from decorates?I don't want to change whole views and old templates.
Thanks in advance.
回答1:
finally i got the solution:
i use django-mobile for this, change in settings.py as per requirement and made a middleware.py for this.
settings.py:(first follow `django-mobile`)
DESKTOP_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates'),)
MOBILE_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates/mobile'),)
then make middleware.py
middleware.py
from django.conf import settings
class MobileTemplatesMiddleware(object):
"""Determines which set of templates to use for a mobile site"""
def process_request(self, request):
# sets are used here, you can use other logic if you have an older version of Python
MOBILE_SUBDOMAINS = set(['m', 'mobile'])
domain = set(request.META.get('HTTP_HOST', '').split('.'))
if request.flavour=='mobile':
settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIRS
else:
settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIRS
回答2:
There is a few ways to solve this problem. First is to find a way to get device info inside view and then
from django.shortcuts import render
def foo_view(request):
data = Model.objects.all()
my_template = 'template.html'
"""
your logic for device_info
"""
if device_info == 'mobile'
my_template='mobile_template.html'
return render(request, my_template, {'data': data})
Other way is to use django-responsive and in this case you can have device_info inside base template so in this case you can have:
{% if device_info.type == 'phone' %}
{% include 'templates/mobile_template.html' %}
{% else %}
{% include 'templates/template.html' %}
{% endif %}
Third way is to rewrite your html/css code and use some responsive CSS frameworks like Foundation or Twitter Bootstrap
Hope it helps
来源:https://stackoverflow.com/questions/34627840/django-webapp-for-mobile-and-desktop