如何使登录表单在Django的整个网站上可用 [英] How to make the login form available on whole site on Django

查看:107
本文介绍了如何使登录表单在Django的整个网站上可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django注册来管理与Django的验证系统的注册和登录/注销。它的工作正常,我有一个基本模板,我已经放置一个登录链接,使用户到另一个页面登录。我想使用实际的登录表单(用户名/密码)替换此链接,因此表单直接显示在每个页面中。任何建议?

I'm using Django registration to manage the registration and login / logout together with Django's auth system. It's working fine, I have a base template where I have put a 'login' link that takes the user to another page to login. I'd like to replace this link with the actual login form (username / password) so the form appears directly in every page. Any suggestions?

推荐答案

您可以使用包含模板标签来构建一个自定义标签,如下所示:

You can use an inclusion template tag to build a custom tag that looks like:

{% login_form %}

您可能想要传递此标签 request.get_absolute_uri ,以便它可以在登录视图的?next = {{request.get_absolute_uri}} 参数中使用它用户可以在登录后重定向到当前页面。

You might want to pass this tag the request.get_absolute_uri so that it can use it in a ?next={{ request.get_absolute_uri }} parameter to the login view so that the user can be redirected to the current page after login.

这样,您可以将登录表单放在任何模板中,而不会违反DRY原则。您甚至可以使用以下方式将其嵌入到基本模板中:

This way, you can put the login form anywhere you want in any template without violating the DRY principles. You can even embed it in your base template using something like:

{% if not user.is_authenticated %}
  {% login_form %}
{% else %}
  {# display welcome message. #}
{% endif %}

这篇关于如何使登录表单在Django的整个网站上可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆