适用于移动设备和台式机的Django WebApp
我用django制作了一个Web应用程序,它在桌面浏览器上看起来非常不错,但是在移动浏览器中,某些页面看起来并不完美.
我的模板目录是myproject/template/
,我想为移动浏览器myproject/template_mobile/
创建新的模板,并且每当网页通过移动设备访问时,它就会重定向到myproject/template_mobile/
模板,否则它将通过myproject/template/
.有什么方法可以使它成为setting.py
还是decorates
?我不想更改整个视图和旧模板.
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.
先谢谢了.
最后,我得到了解决方案:
我为此使用 django-mobile ,根据要求更改settings.py
并做了一个
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'),)
然后制作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