sha*_*ank 3 python django mobile django-templates
我用django制作了Web应用程序,它在桌面浏览器上看起来非常不错,但是在移动浏览器中,某些页面看起来并不完美。我的模板目录是myproject/template/我想要为移动浏览器创建新模板,myproject/template_mobile/并且每当网页通过移动设备访问时,它就会重定向到myproject/template_mobile/模板,否则它将进入低谷myproject/template/。有没有什么办法,使之形成setting.py或decorates?我不想改变整体的观点和旧模板。
提前致谢。
最终我得到了解决方案:我为此使用django-mobile,settings.py根据要求进行了更改,并为此做了一个middleware.py。
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'),)
Run Code Online (Sandbox Code Playgroud)
然后做 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4446 次 |
| 最近记录: |