1

I'm trying to get POST from 2 diferents dropdown, I had get the parametres from the POST, but i get problems with the CSRF token ....

index.html

<form method="post" action="/getdata/">{% csrf_token %}
    <select name="Lista">
        <option selected="selected" disabled>Objects on page:</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
        <option value="40">40</option>
        <option value="50">50</option>
    </select>

    <select name="Lista2">
        <option selected="selected" disabled>Objects on page:</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
        <option value="40">40</option>
        <option value="50">50</option>
    </select>    
    <input type="submit" value="Select">    
</form>

in spite of I use the csrf token in my html form, it didn't worked ...

views.py

from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render

from view.forms import *
from django.shortcuts import render_to_response, redirect
from view.models import *



def index(request):   
    if request.method == 'POST':        
        Lista = request.POST.get('Lista')
        print "Lista 1 "+Lista
        Lista2 = request.POST.get('Lista2')
        print "Lista 2 "+Lista2

        #FORMS
        form = FormsLista(request.POST)
        if form.is_valid():                
            newPost = Lista(num_lista_1=Lista, num_lista_2=Lista2)                
            newPost.save()              
            context = {                    
                'Lista': Lista,
                'Lista2': Lista2
            }       

            return render(request, 'showdata.html', context)
    else:

        template = loader.get_template('index.html')
        return HttpResponse(template.render())

models.py

from django.db import models

class Lista (models.Model):
    num_lista_1 = models.CharField(max_length=100, null=True)
    num_lista_2 = models.CharField(max_length=100, null=True)
    def __unicode__(self):
        return self.num_lista_1

I have the cookies activated by the way ...

1 Answer 1

1

To use Django's CSRF protection, you need to render the template with the request object:

template = loader.get_template('index.html')
return HttpResponse(template.render(request=request))

Your code would be more consistent if you used render everywhere:

return render(request, 'index.html')

To fix the [view] didn't return an HttpResponse error, you need to make sure that the index view always returns an HttpResponse. At the moment you return None for POST requests when the form is not valid. It is very common in Django to do the following:

def index(request):   
    if request.method == 'POST':
        ...
    form = FormsLista()
    return render(request, 'index.html', {'form': form})

Then, in your template, you can display the form errors. See the docs on rendering forms for more info.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried both options ... but I get this error The view tutorial.views.index didn't return an HttpResponse object. It returned None instead. ...
That's a separate issue. As the error suggests, you always need to return a response. Look at what happens when if request.method == 'POST':, but the form is not valid.
It worked! ... but I don't understand why always my POST is not valid ... some idea?
If you look at form.errors it should give you a hint. If you're still stuck, please ask a new question because it's a separate issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.