'str'对象没有属性'get'django错误 [英] 'str' object has no attribute 'get' django error

查看:63
本文介绍了'str'对象没有属性'get'django错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a login form and after filling up the information it is showing 'str' object has no attribute 'get' error views.py file:

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')

推荐答案

由于此行而发生错误:

               return("home")

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

In order to return a string, you must use:

    from django.http import HttpResponse

然后

               return HttpResponse("home")

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

However, maybe you wanted to redirect to a view called 'home', and mistakenly forgotten redirect.

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

In that case just correct your code to:

               return redirect("home")

这篇关于'str'对象没有属性'get'django错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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