using the request object inside a form

3 views
Skip to first unread message

vanderkerkoff

unread,
Aug 2, 2011, 10:47:59 AM8/2/11
to Django users
Hello there.

I need to use parts of the URL to calculate some thing inside my form.

Here's a stripped down version, I can't seem to get it to give me
anything other than none for self.request :-(

class EventBookForm(ModelForm):
title = CharField()
firstname = CharField()
surname = CharField()
address = CharField(widget=Textarea(attrs={'rows': 5, 'cols': 80}))
postcode = CharField()
telephone = CharField()
email = EmailField()
workshops = MultipleChoiceField()
requirements = CharField(widget=Textarea(attrs={'rows': 5, 'cols':
80}))

class Meta:
model = Event

def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
print self.request
super(EventBookForm, self).__init__(*args, **kwargs)
workshop_choices = []
data = Workshop.objects.filter("some rules based on what I get back
from the request object, which is currently none")
for d in data:
workshop_choices.append((d.title,d.title))

self.fields['workshops']=MultipleChoiceField(widget=CheckboxSelectMultiple,
choices=workshop_choices, required=False)

I'm pretty certain I'm doing something unbelievably stupid but I am
trying, honestly!

Anyone got any ideas?

Daniel Roseman

unread,
Aug 2, 2011, 12:22:43 PM8/2/11
to django...@googlegroups.com
The problem isn't in the code you've posted, but in how you instantiate the form in your view. You need to actually pass in 'request' as a keyword argument.
--
DR.

vanderkerkoff

unread,
Aug 2, 2011, 1:36:36 PM8/2/11
to Django users
Thanks Daniel

I told you I was doing something stupid :-)

V
Reply all
Reply to author
Forward
0 new messages