apardes
apardes

Reputation: 4380

Django view receives GET Request from form that should be sending POST

I have a view that is supposed to handle the submission of a form. The HTML form in the template is supposed to be sending a post however the view only ever receives a GET request.

View:

def eventSell(request, id):
    event = Event.objects.get(pk = id)
    if request.user.is_authenticated():
        print request.user 
        if request.method == ['POST']:
            print 'post'
            form = ListingForm(request.POST)
            if form.is_valid():
                print 'form is valid'
                user = request.user
                price = request.POST['price']
                t = Object(event = event, price = price, seller = user, date_listed = timezone.now())
                t.save()
                return HttpResponseRedirect(reverse('app:index'))
        else:
            print 'get'
            form = ListingForm()
        return render_to_response('app/list.html', {'form' : form, 'event' : event}, context_instance = RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('allauth.accounts.views.login'))

Template:

<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>

I'm really stumped here so any advice would be really appreciated. Thanks.

Upvotes: 4

Views: 1474

Answers (1)

karthikr
karthikr

Reputation: 99670

It is probably sending a POST however, you are not listening correctly.

if request.method == ['POST']:

should be

if request.method == 'POST':

Or just

if request.POST:

One more thing.

You can use the @login_required decorator instead of manually checking for authenticated users.

Upvotes: 6

Related Questions