修改什么/在哪里修改 django-registration 以与移动浏览器一起使用

发布于 2024-11-28 14:35:43 字数 441 浏览 1 评论 0原文

我使用 django-registration 和 django auth 来创建和登录客户帐户。

我们的网站将由移动用户和桌面用户使用。我们刚刚开始通过根据用户代理字符串从视图加载不同的模板来解决移动用户的想法。它做得很干净,但我不确定这是否是正确的方法,因为我们现在陷入了对不易访问的视图(我们没有自己编写)的操作上。

这让我想到了眼前的问题: 我不知道如何解决将移动用户从 django-registration/auth 发送到的登录网址(桌面版本)重定向的问题。

我可以改变策略并解决模板文件本身中的不同浏览器问题。感觉很快就会变得混乱。我一点也不喜欢这个主意!

或者我继续使用当前的方法,即根据用户代理字符串使用不同的模板呈现请求。然后我需要知道应该如何处理 django 注册(如何根据用户代理字符串加载一组不同的模板)。如果只是为了使更新模块更容易,我宁愿不更改 django 注册代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒的傷心 2024-12-05 14:35:43

django 注册模板非常简单,很少使用。我只是将这些作为特殊情况处理,然后提出一个在两个平台上都能很好地工作的 base.html。

我的注册页面看起来很简单,很多网站都这样做,这并不意外。

另一种选择是我们使用一个中间件,它根据检测是否是移动设备来设置模板目录。您可以像这样检测移动浏览器 检测移动浏览器(不仅仅是iPhone) 在 python 视图 中,然后有一个中间件,使用 make_tls_property 技巧来更新 TEMPLATE_DIRS ,如下所示:

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

需要明确的是,make_tls_property,它是djangotoolbox,使 TEMPLATE_DIRS 设置每个线程变量而不是全局变量,以便每个请求响应循环获取它自己的变量“版本”。

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.

与往事干杯 2024-12-05 14:35:43

一种方法是简单地编写自己的登录视图,该视图调用 django-registration 视图来完成艰苦的工作,但根据上下文向其传递不同的模板:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文