【问题标题】:'str' object has no attribute 'get' django error'str' 对象没有属性 'get' django 错误
【发布时间】:2020-07-24 13:58:20
【问题描述】:

我正在创建一个登录表单,并在填写信息后显示“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')

【问题讨论】:

    标签: django authentication


    【解决方案1】:

    由于这一行而发生错误:

                   return("home")
    

    为了返回一个字符串,你必须使用:

        from django.http import HttpResponse
    

    然后,

                   return HttpResponse("home")
    

    然而,也许您想重定向到一个名为“home”的视图,却错误地忘记了redirect

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

                   return redirect("home")
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多