适用于移动设备和台式机的Django WebApp

sha*_*ank 3 python django mobile django-templates

我用django制作了Web应用程序,它在桌面浏览器上看起来非常不错,但是在移动浏览器中,某些页面看起来并不完美。我的模板目录是myproject/template/我想要为移动浏览器创建新模板,myproject/template_mobile/并且每当网页通过移动设备访问时,它就会重定向到myproject/template_mobile/模板,否则它将进入低谷myproject/template/。有没有什么办法,使之形成setting.pydecorates?我不想改变整体的观点和旧模板。

提前致谢。

sha*_*ank 5

最终我得到了解决方案:我为此使用django-mobilesettings.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)

  • 请至少在此处提及原作者,而不要只是复制和粘贴其他站点的代码!你的代码和[这篇文章](http://www.codekoala.com/posts/how-i-have-mobile-desktop-site-django/)非常相似 (3认同)