-->

What/Where to modify django-registration to use wi

2019-04-09 12:52发布

问题:

I am using django-registration along side django auth for my client account creation and login.

Our site will be used by moble users and desktop users. We just started tackling the idea of mobile users by loading different templates from the view depending on user agent strings. It's cleanly done, but I am not sure if it is the right way to do it as we are now stuck with what to do on views that are not easily accessible (that we didn't write ourselves).

Which brings me to the problem at hand: I have no idea how to tackle redirecting the mobile user away from the login url that django-registration/auth sends them to (the desktop version).

I could change tactics and tackle the different browsers in the template files themselves. That feels like it is going to get messy fast. I don't like that idea at all!

Or I stay with my current method, which is to render the request with different templates based on user agent strings. Then i need to know how I should be dealing with django-registration (how to load a different set of templates based on the user agent string). I would rather not change the django-registration code, if just to make updating modules easier.

回答1:

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.



回答2:

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)