What/Where to modify django-registration to use with mobile broswers

假如想象 提交于 2019-12-04 10:56:10
Rob Osborne

The django registration templates are very simple and are used very rarely. I simply handle these as special cases and just come up with a base.html for that works on both platforms reasonably well.

My registration pages look very simple, many sites do this and it is not unexpected.

Another option is to us a middleware which sets the template directory based upon detecting if it is a mobile device. You can detect the mobile browser like this Detect mobile browser (not just iPhone) in python view and then have a middleware that uses the make_tls_property trick to update the TEMPLATE_DIRS something like this:

TEMPLATE_DIRS = settings.__dict__['_wrapped'].__class__.TEMPLATE_DIRS = make_tls_property(settings.TEMPLATE_DIRS)

class MobileMiddleware(object):
    """Sets settings.SITE_ID based on request's domain"""
    def process_request(self, request):
        if *mobile*:
            TEMPLATE_DIRS.value = *mobiletemplates* + settings.BASE_TEMPLATE_DIRS
        else:
            TEMPLATE_DIRS.value = *normaltemplates* + settings.BASE_TEMPLATE_DIRS

Just to be clear, make_tls_property, which is part of djangotoolbox, makes the TEMPLATE_DIRS setting a per thread variable instead of a global variable so each request response loop gets it's own "version" of the variable.

One method is to simply write your own login view that calls the django-registration view to do the hard work, but passing it a different template depending on the context:

def login(request, *args, **kwargs):
    my_kwargs = kwargs.copy()
    if <mobile condition>:
        my_kwargs['template_name'] = 'my_app/some_template.html'
    else:
        my_kwargs['template_name'] = 'my_app/some_other_template.html'

    from django.contrib import auth
    return auth.login(request, *args, **my_kwargs)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!