View Django receives a GET request from a form that should send a POST

I have a view that should handle the form view. The HTML form in the template is supposed to send a message, but 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 fixated, so any advice would be really appreciated. Thanks.

+4
source share
1 answer

Most likely it sends a POST , you are not listening correctly.

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

it should be

 if request.method == 'POST': 

Or simply

 if request.POST: 

One more thing.

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

+6
source

Source: https://habr.com/ru/post/1497073/


All Articles