如何在Django中登录用户?

如何在Django中登录用户?,django,login,Django,Login,我目前正在做一个Django项目,以便更熟悉Django。目前,我正在尝试制作两个模板,一个是主页,带有登录页面的链接,它将登录用户,然后返回模板并显示其他内容(将通过nginx处理),另一个模板只有在登录时才能访问。出于某种原因,但似乎不起作用,或者说登录不起作用 这是views.py文件: from django.shortcuts import render_to_response, get_object_or_404, HttpResponse from django.contrib.a

我目前正在做一个Django项目,以便更熟悉Django。目前,我正在尝试制作两个模板,一个是主页,带有登录页面的链接,它将登录用户,然后返回模板并显示其他内容(将通过nginx处理),另一个模板只有在登录时才能访问。出于某种原因,但似乎不起作用,或者说登录不起作用

这是views.py文件:

from django.shortcuts import render_to_response, get_object_or_404, HttpResponse
from django.contrib.auth import *
from django.contrib.auth.decorators import login_required

def index(request):
        return render_to_response('index.html')

@login_required
def main(request):
    return render_to_response('loginrequired.html')
这是主模板:

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
Welcome {{user.username}}. You're now logged in as required.<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
<html>
<head>
    <title>User Login</title>
</head>
<h1>User Login</h1>
{% if form.errors %}
    <p>Your username and password didn't match.
        Please try again.</p>
{% endif %}
<form method="post" action="." >
{% csrf_token %}
    <p><label for="id_username">Username:</label>
        {{ form.username }}</p>
    <p><label for="id_password">Password:</label>
        {{ form.password }}</p>
    <input type="hidden" name="next" value="/" />
    <input type="submit" value="login" />
</form>
</body>
</html>
{%load static%}
{%get_static_prefix作为static_prefix%}
Django NGINX试验
Django NGINX试验


{%if user.u经过身份验证%} {{user.username} {%else%} {%endif%}
这是登录所需的模板:

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
Welcome {{user.username}}. You're now logged in as required.<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
<html>
<head>
    <title>User Login</title>
</head>
<h1>User Login</h1>
{% if form.errors %}
    <p>Your username and password didn't match.
        Please try again.</p>
{% endif %}
<form method="post" action="." >
{% csrf_token %}
    <p><label for="id_username">Username:</label>
        {{ form.username }}</p>
    <p><label for="id_password">Password:</label>
        {{ form.password }}</p>
    <input type="hidden" name="next" value="/" />
    <input type="submit" value="login" />
</form>
</body>
</html>
{%load static%}
{%get_static_prefix作为static_prefix%}
Django NGINX试验
Django NGINX试验


欢迎{{user.username}。您现在已按要求登录。

{%if user.u经过身份验证%} {{user.username} {%else%} {%endif%}
最后,但并非最不重要的是我的登录模板:

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

<html>
<head>
<title>Django NGINX Test</title>
</head>
<body>
<h1>Django NGINX Test</h1>
<img src="{{STATIC_PREFIX}}beach.jpg"/>
<BR><BR>
Welcome {{user.username}}. You're now logged in as required.<BR><BR>
<h2>
{% if user.is_authenticated %}
  <a href="test">Log out</a> {{user.username}}
{% else %}
  <a href="login">Log in</a>
{% endif %}
</h2>
</body>
</html>
<html>
<head>
    <title>User Login</title>
</head>
<h1>User Login</h1>
{% if form.errors %}
    <p>Your username and password didn't match.
        Please try again.</p>
{% endif %}
<form method="post" action="." >
{% csrf_token %}
    <p><label for="id_username">Username:</label>
        {{ form.username }}</p>
    <p><label for="id_password">Password:</label>
        {{ form.password }}</p>
    <input type="hidden" name="next" value="/" />
    <input type="submit" value="login" />
</form>
</body>
</html>

用户登录
用户登录
{%if form.errors%}
您的用户名和密码不匹配。
请再试一次

{%endif%} {%csrf_令牌%} 用户名: {{form.username}

密码: {{form.password}}


我将感谢任何帮助。我已经为此学习了Django教程,但我没有理解。

您需要确保在
设置中包含以下中间件。py

  • 会话中间件
  • 认证中间件
要确保名为
user
的上下文变量可用于模板,需要确保
django.contrib.auth.context\u processors.auth
上下文处理器位于
settings.py


更多信息可以在中找到。

您需要确保在
设置中包含以下中间件。py

  • 会话中间件
  • 认证中间件
要确保名为
user
的上下文变量可用于模板,需要确保
django.contrib.auth.context\u processors.auth
上下文处理器位于
settings.py


更多信息可以在中找到。

用户已登录,但您没有将其传递给模板,因此它不会显示用户详细信息。确保使用RequestContext呈现模板,或者使用新的
render
快捷方式,而不是
render\u to\u response
用户已登录,但您没有将其传递给模板,因此它不会显示用户详细信息。确保使用RequestContext呈现模板,或者使用新的
render
快捷方式,而不是
render\u to\u response

,以何种方式不起作用?当你尝试进入需要登录的页面时会发生什么?好吧,当我尝试访问需要登录的模板时,它只向我显示它,而不要求登录。我不确定,如果我已经登录,但它也不显示我的用户名,或者我必须以某种方式将其传递给视图吗?以何种方式不起作用?当你尝试进入需要登录的页面时会发生什么?好吧,当我尝试访问需要登录的模板时,它只向我显示它,而不要求登录。我不确定,如果我已经登录,但它也不显示我的用户名,或者我必须以某种方式将其传递给视图吗?附议这个答案:如果你不使用上面链接中列出的表单和方法,你会让事情变得更难。+1因为@Tom提到的原因,另外,你应该使用快捷方式确保包含
RequestContext
。附议这个答案:如果你不使用上面链接中列出的表单和方法,你会让事情变得更困难。+1因为@Tom提到的原因,另外,您应该使用快捷方式确保包含
RequestContext