Sunday, September 1, 2013

Re: Passing initial values to a form

The URL parameter (iid) is accessible in your view in self.kwargs["iid"].

You can pass it to the form by overriding your view's `get_initial` method (https://github.com/django/django/blob/1.6b2/django/views/generic/edit.py#L22)


`get_initial` should return a dict, and you should use:

{
    'name_of_the_model_field': self.kwargs["iid"]
}


On Sun, Sep 1, 2013 at 6:37 PM, Drew Ferguson <drew@afccommercial.co.uk> wrote:
Hi

I have gotten myself badly confused today trying to figure this out.

If I have these elements

# urls.py
url(r'^banker/(?P<iid>\d+)/iac$', view = IacCreateView.as_view())

# views.py
class IacCreateView(LoginRequiredMixin, CreateView):
    template_name = 'iaccount_form.html'
    model = Iaccount
    form_class = IaccountForm

# forms.py
class IaccountForm(forms.ModelForm):
    class Meta(object):
        model = Iaccount

How can I use a value (iid) from the url to set a field's initial
value in the form

or more generally pass initial values into the form from a previous page
--
Drew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment