cleaned_data in forms.py?

39 views
Skip to first unread message

Leandro Alves

unread,
Jul 10, 2012, 6:30:09 AM7/10/12
to django...@googlegroups.com
Hi, 

I'm new in Django and I'm struggling myself here to find out how to get the value of a field inside the forms.py. 
Yes, I did read the documentation, keep reading and searching on the internet... But please, could someone give a tip regarding this? 

I just want to get/use the values of the fields from ContactForm1 into ContactForm2? Is that possible? 

I know that in the views.py, we can get it from the "cleaned_data"... but how in the forms.py? 

from django import forms

class ContactForm1(forms.Form):
    subject = forms.CharField(max_length=100)
    sender = forms.EmailField()

class ContactForm2(forms.Form):
    message = forms.CharField(widget=forms.Textarea)

For example I want to be able to use the value of the "sender" inside the ContactForm2, showing the message box just for some specific senders... 

Please anyone with a simple tip? 

Thanks in advance,

Leandro 

Сергей Фурсов

unread,
Jul 10, 2012, 6:56:39 AM7/10/12
to django...@googlegroups.com
Why not use Form Wizard from django.contrib.formtools.
Simply change show_message_form_condition method to something like
def show_message_form_condition(wizard):
    cleaned_data = wizard.get_cleaned_data_for_step('0') or {}
    return cleaned_data.get('sender', '') in ['adm...@example.com', 'speci...@example.com']



2012/7/10 Leandro Alves <lda...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/v_d4gPDBOy4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Leandro Alves

unread,
Jul 10, 2012, 7:25:12 AM7/10/12
to django...@googlegroups.com
Hi,

Yes... it is exactly with the form wizard that I'm trying to do it..

But how can I "receive" these values inside my forms.py? This is what I couldn't find yet. 

I want to be able use this values inside the ContactForm2 form. 

Thanks for your help so far. 

Leandro




2012/7/10 Leandro Alves <lda...@gmail.com>
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Сергей Фурсов

unread,
Jul 10, 2012, 7:45:38 AM7/10/12
to django...@googlegroups.com
You can try to override get_form_kwargs method

def get_form_kwargs(self, step):
    if step == '1'
        cleaned_data = self.get_cleaned_data_for_step('0') or {}
        return {'sender': cleaned_data.get('sender', None)}
    else:
        return {}

and in ContactForm2 override __init__ method with kwargs, for example:

class ContactForm2(forms.Form):
    def __init__(self, *args, **kwargs):
        sender = kwargs.pop('sender', None)
        if sender in ['ema...@example.com', ]:
            # some actions
        super(ContactForm2, self).__init__(*args, **kwargs)

    message = forms.CharField(widget=forms.Textarea)

I didn't try it myself, hope this will work)

2012/7/10 Leandro Alves <lda...@gmail.com>
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/uOZzfAUMgDgJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Sergiy Khohlov

unread,
Jul 10, 2012, 8:09:59 AM7/10/12
to django...@googlegroups.com
Sometimes ago I've added a post to my blog about this:

http://skhohlov.blogspot.com/2012/04/passing-values-from-view-to-form.html

2012/7/10 Сергей Фурсов <geys...@gmail.com>:
>>>> django-users...@googlegroups.com.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/django-users?hl=en.
>>>
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/django-users/-/uOZzfAUMgDgJ.
>>
>> To post to this group, send email to django...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.

Leandro Alves

unread,
Jul 10, 2012, 8:56:01 AM7/10/12
to django...@googlegroups.com
Hi Сергей Ф,

It worked now!!! \o/  

But I have a question... in this case why do I need to send "None" in "get_form_kwargs" and why should I use the  "super(ContactForm2, self).__init__(*args, **kwargs)" after the init? 

Thank you very much for your help!!

Leandro

Leandro Alves

unread,
Jul 10, 2012, 8:58:18 AM7/10/12
to django...@googlegroups.com
Hi skhohlov,

I will read it right now!!!!

Thanks for sharing. 

Leandro
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/django-users?hl=en.
>>>
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/django-users/-/uOZzfAUMgDgJ.
>>
>> To post to this group, send email to django...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

kenneth gonsalves

unread,
Jul 10, 2012, 9:00:34 AM7/10/12
to django...@googlegroups.com
On Tue, 2012-07-10 at 05:56 -0700, Leandro Alves wrote:
> But I have a question... in this case why do I need to send "None" in
> "get_form_kwargs"

None is the default value to prevent a crash
> and why should I use the "super(ContactForm2, self).__init__(*args,
> **kwargs)" after the init?

well, that is python syntax for inheritance

(both questions relate to python syntax)
--
regards
Kenneth Gonsalves

Leandro Alves

unread,
Jul 10, 2012, 9:06:02 AM7/10/12
to django...@googlegroups.com
Read and understood!! :)

Thanks, Kenneth!!

Leandro

Sergiy Khohlov

unread,
Jul 10, 2012, 10:23:10 AM7/10/12
to django...@googlegroups.com
its constructor. You shoul use this because parent constructor should be save

thanks, Serge
(or Sergio for you :-) )

2012/7/10 Leandro Alves <lda...@gmail.com>:
>>>>> django-users...@googlegroups.com.
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/django-users?hl=en.
>>>>
>>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Django users" group.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msg/django-users/-/uOZzfAUMgDgJ.
>>>
>>> To post to this group, send email to django...@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> django-users...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/django-users?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/I_nKu8OfgpgJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages