--
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/-/1z9ElVqpRucJ.
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.
1) set initial values in view class
class myviewclass():
""" This is example only . full class realization is more complex """
form_class = myformclass
template_name = "mytemplate.html"
get_initial(self):
""" Passing initial values to the form"""
# At first initial data should be saved
super(myviewclass, self).get_initial()
# adding our value
self.initial= {"myuser": self.request.user}
return self.initial
Second part (form side)
myformclass():
""" This is really simple example. Perfect form is more complex"""
def __init__(self, *args, **kwargs):
# get our values from view
myvalues = kwargs.pop('initial', None)
#print it to console (this is a dict by default)
print myvalues
# and executing default constructor after this
super(myformclass, self).__init__(*args, **kwargs)
Thanks, Serge
2012/3/11 Donald Stufft <donald...@gmail.com>:
Of course form does not have access to the object.
1. Because there are no paths to do it automatically, this would require
passing the request object to all forms explicitly
2. Because, as Donald noted, forms don't *need* a request object and
indeed can be used completely independently from the request/response
cycle (or from a given request).
3. Plus what would be the semantics there, would the form prime itself
using request.POST? request.GET? A combination of both? Only under some
request methods?
Views exist to handle requests, that's all their job is, so them
receiving a request object makes perfect sense. Not so for forms.
> To put the question another way, when we define a view function, we
> do something like:
>
> def edit(request,story_id):
> ...
>
> But when we create a form, we can't just do:
>
> class StoryForm(ModelForm, request):
> …
For what it's worth, this code makes absolutely no sense, it tries to
create a class which inherits from a request object (which is not in
scope and is not a type)
On Sunday, March 11, 2012 6:24:30 AM UTC-7, skhohlov wrote:Of course form does not have access to the object.skholov - Thanks, but you misunderstand my question. Again, I know that forms don't have access to request, and again, I've got it working already (though with a different approach - see my OP). My question was an attempt to understand the plumbing or the python better. To reiterate, I'd like to know WHY request isn't automatically available from forms, as it is from views. To put the question another way, when we define a view function, we do something like:def edit(request,story_id):...
But when we create a form, we can't just do:class StoryForm(ModelForm, request):...
It seems like this is the kind of thing Django could take care of for us - we shouldn't have to jump through hoops to get at request - but there's probably a good reason why it works this way. Does my question make more sense now?Thanks,Scot
--
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/-/8DXqXSX-YhAJ.
2012/3/12 shacker <sha...@birdhouse.org>:
> --
> 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/-/G0UkLvyadAgJ.
If your form requires access to the request object, or to the
currently logged in user, it should simply take that object as an
additional required argument to the form constructor. There should be
no need for any magic here.
Cheers
Tom