'str'对象没有属性'get'django错误

时间:2020-07-24 13:58:20

标签: django authentication

我正在创建一个登录表单,并在填写完信息后显示“ str”对象没有属性“ get”错误 views.py文件:

from django.shortcuts import render,redirect
from django.contrib import messages
from django.contrib.auth.models import User, auth

# Create your views here.
def login(request):
    if request.method == 'POST':
        username= request.POST['username']
        password = request.POST['password']
        user = auth.authenticate(username=username,password=password)
        if user is not None:
           auth.login(request,user)
           return("home")
        else:
           messages.info(request,"invalid credentials")
           return redirect('login')

    else:
        return render(request,'buy/login.html')

1 个答案:

答案 0 :(得分:1)

由于此行而发生错误:

               return("home")

要返回字符串,必须使用:

    from django.http import HttpResponse

然后

               return HttpResponse("home")

但是,也许您想重定向到名为“ home”的视图,并错误地忘记了redirect

在这种情况下,只需将代码更正为:

               return redirect("home")