“如果request.method =='POST':'” == False,但是“ if request.POST”; ==正确。为什么?-Django [英] "if request.method == 'POST':" == False, but "if request.POST" == True. Why?- Django

查看:355
本文介绍了“如果request.method =='POST':'” == False,但是“ if request.POST”; ==正确。为什么?-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下代码时,第一个if语句始终返回False。但是,如果我将其更改为request.POST,它将返回True。有人知道为什么吗?其他人有没有经历过?我正在使用方法= post的基本形式发送数据。

When I use the following code, the first if statement always returns a False. But if I change it to request.POST it will return a True. Does anyone know why? Has anyone else experienced this? I'm sending it data using a basic form with method="post".

def add_new_user(request):
    context = RequestContext(request)
    if request.method == 'POST':
        form = NewUserForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return index_input(request)
        else:
            print form.errors
    else:
        form = NewUserForm()

    return render_to_response('appname/add_new_user.html',
        {'form': form}, context)


推荐答案

一个空字典将在Python中返回 False ,这就是为什么 request.POST的原因如果POST QueryDict中没有数据,将返回 False

An empty dictionary will return False in Python, which is why request.POST would return False if there is no data in the POST QueryDict.

$ d = {}
$ d is True
$ False

这篇关于“如果request.method =='POST':'” == False,但是“ if request.POST”; ==正确。为什么?-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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