Red-Tune-84
Red-Tune-84

Reputation: 387

"if request.method == 'POST':" == False, but "if request.POST" == True. Why?- Django

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)

Upvotes: 1

Views: 5880

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34593

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

Upvotes: 1

Related Questions