Python Django is_authenticated()在索引页上始终返回false,但在其他页上不返回false

Python Django is_authenticated()在索引页上始终返回false,但在其他页上不返回false,python,django,authentication,Python,Django,Authentication,我在我的网站的不同页面上使用相同的layout.html,其中包括登录检查 {% if user.is_authenticated %} Signed in as <strong>{{ user.username }}</strong>. {% else %} Not signed in. {% endif %} 我的应用程序/url.py: from django.urls

我在我的网站的不同页面上使用相同的layout.html,其中包括登录检查

        {% if user.is_authenticated %}
            Signed in as <strong>{{ user.username }}</strong>.
        {% else %}
            Not signed in.
        {% endif %}
我的应用程序/url.py:

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name = "index"),
    path("login", views.login_view, name = "login"),
    path("logout", views.logout_view, name = "logout"),
    path("register", views.register, name = "register"),
    path("lobby/<str:lobby_name>/", views.lobby, name = "lobby")
]
我的应用程序/views.py

from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.shortcuts import redirect
from django.http import Http404
from django.http import HttpResponseForbidden
from django import forms
from io import BytesIO
from PIL import Image
from django.contrib.auth.decorators import login_required
from .models import *


def index(request):
    return render(request, "app/index.html",{
        "user": User.username
    })


def login_view(request):
    if request.method == "POST":
        
        user = authenticate(request, username=request.POST["username"], password=request.POST["password"])

        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "app/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "app/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "app/register.html", {
                "message": "Passwords must match."
            })

        # attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "app/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request, "app/register.html")

如果有人知道解决方案,我们将不胜感激

修复:

def索引(请求):
返回渲染(请求“app/index.html”{
“用户名”:request.user.username#不覆盖用户
})
您似乎正在将用户名传递给user属性,从而覆盖默认的
user
对象

呈现模板RequestContext时,当前登录的用户(用户实例或匿名用户实例)存储在模板变量{{user}}中:


--从django文档:

问题在于索引方法

def index(request):
    return render(request, "app/index.html",{
        "user": User.username
    })
换成

def index(request):
    return render(request, "app/index.html")

在模板中,您可以通过{{user.username}访问登录用户的用户名。因此,无需覆盖默认对象来检查用户是否经过身份验证,您无需传递用户对象。您已跳过django的默认对象。因此,我的建议是,不要传递user对象,它将正常工作。它将是
{{user.username}
,只有在没有
请求的情况下才可以。
def index(request):
    return render(request, "app/index.html",{
        "user": User.username
    })
def index(request):
    return render(request, "app/index.html")