Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden field in Django form not in cleaned_data

I have this form:

class CollaboratorForm(forms.Form):
    user = forms.CharField(label="Username",max_length=100)
    canvas = forms.IntegerField(widget=forms.HiddenInput)
    ....
    def clean_user(self):
        user = self.cleaned_data['user']
        canvas = self.cleaned_data['canvas']

In the view I'm simply calling

if form.is_valid():

I get the error:

KeyError at /canvas/1/add-collaborator/
'canvas'

According to firebug the value is posting, it's just doesn't seem to be making it to my clean function. Am I doing it wrong?

EDIT: Post data

canvas  1
csrfmiddlewaretoken 2cb73be791b32ca9a41566082c804312
user    username

EDIT2: I would also be willing to take an answer that could tell me how to send the primary key to the clean_user function, where the primary key is the /1/ in the example url above. The function in the view that is called is:

def canvas_add_collaborator(request, pk):

So I would want to send the pk to the clean_user function which would solve my problem by not needing the hidden field.

like image 840
Samsquanch Avatar asked Nov 30 '11 15:11

Samsquanch


People also ask

What does Cleaned_data do in Django?

cleaned_data is where all validated fields are stored.

How do hidden fields work?

How do Hidden Fields work? Hidden Fields allows you to place data that you already have directly in your typeform URL. You can view all your data in the Results panel.

What are hidden fields?

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

How to use get and POST method in django?

GET and POSTDjango's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. GET , by contrast, bundles the submitted data into a string, and uses this to compose a URL.


1 Answers

You need to change the method name to clean(), not clean_user(). 'canvas' is not in the cleaned_data if you are just validating the user field.

like image 70
Chris Lacasse Avatar answered Oct 26 '22 05:10

Chris Lacasse