栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在每个页面上放置一个django登录表单

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在每个页面上放置一个django登录表单

好的,我最终找到了一种方法,尽管我确信还有更好的方法。我创建了一个新的中间件类,称为LoginFormMiddleware。在process_request方法中,或多或少使用auth登录视图的方式处理表单:

class LoginFormMiddleware(object):    def process_request(self, request):        # if the top login form has been posted        if request.method == 'POST' and 'is_top_login_form' in request.POST: # validate the form form = AuthenticationForm(data=request.POST) if form.is_valid():     # log the user in     from django.contrib.auth import login     login(request, form.get_user())     # if this is the logout page, then redirect to /     # so we don't get logged out just after logging in     if '/account/logout/' in request.get_full_path():         return HttpResponseRedirect('/')        else: form = AuthenticationForm(request)        # attach the form to the request so it can be accessed within the templates        request.login_form = form

现在,如果你已安装了请求上下文处理器,则可以使用以下方式访问表单:

{{ request.login_form }}

请注意,在表单中添加了一个隐藏字段“ is_top_login_form”,因此我可以将其与页面上其他已发布的表单区分开。此外,表单动作为“。” 而不是auth登录视图。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402413.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号