“ WSGIRequest”对象没有属性“ get” [英] 'WSGIRequest' object has no attribute 'get'

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

问题描述

我正在尝试使用Django创建登录表单。我正在创建一个视图,巫婆将同时处理获取和发布请求。

I'm trying to create a login form with Django. I'm creating a view witch will handle both get and post requests for login.

这里是我的设计方式:

class Login(View):
    def get(self,request):
        c = {}
        c.update(csrf(request))
        return render_to_response("login.html", c)
    def post(self,request):
        username = request.get('username','')
        password = request.get('password','')
        user = auth.authenticate(username = username, password = password)
        if(user is not None):
            auth.login(request,user)
            return True
        else:
            return False

我可以获得此表格,但是当我发布时我得到了:

I can get this form, but when i post i am getting:

'WSGIRequest' object has no attribute 'get'

错误。设计此类视图的正确方法是什么?

Error. What is the correct way to design such views?

推荐答案

您应该使用 request.POST 字典式对象:

You should use the request.POST dict-like object:

username = request.POST.get('username','')
password = request.POST.get('password','')

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

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