crispy forms two forms and context

322 views
Skip to first unread message

Brad Rice

unread,
Sep 1, 2014, 4:08:55 PM9/1/14
to django...@googlegroups.com
This document seems to indicate I can have two forms inside one set of form tags:


I cannot figure out how to set the context to be able to iterate over both forms.

{% crispy form %}

works on my model form.

If I try to set context in my view like this:

def get_context_data(self, **kwargs):
        context = super(ApplicationVerify, self).get_context_data(**kwargs)
        context['app'] = Application.objects.get(id=self.kwargs['app_id'])
        context['answers'] = Answers.objects.get(created_by=self.request.user)
        context['applicant'] = Applicant.objects.get(created_by=self.request.user)
        return context

and then use instead of crispy form

{% crispy answers %}

I get errors.

'Answers' object has no attribute 'fields'

How do I set a form to have context from several models?

I do see the context is coming into the template, it just is not treating them as having form widgets.

James Schneider

unread,
Sep 1, 2014, 4:15:39 PM9/1/14
to django...@googlegroups.com
It looks like you are passing the context an actual Answers object, not a Form or ModelForm object that will add/update an Answers object, hence the reason the trace back complains about a lack of 'fields'.

-James
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9845f147-8c82-49fd-b15a-62e79680caa2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brad Rice

unread,
Sep 1, 2014, 8:21:00 PM9/1/14
to django...@googlegroups.com
Here is my entire view class to go with a ModelForm. How do I get the various forms as context into my template?

class ApplicationVerify(UpdateView):
    model = Answers
    form_class = ApplicationCheckForm
    template_name = 'requestform/application_detail.html'

    @method_decorator(login_required(login_url='/accounts/login/'))
    def dispatch(self, *args, **kwargs):
        return super(ApplicationVerify, self).dispatch(*args, **kwargs)

    def get_object(self, queryset=None):
        return Answers.objects.get(created_by=self.request.user)

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.created_by = self.request.user
        obj.save()
        a = Application.objects.get(created_by=self.request.user)
        b = Answers.objects.get(created_by=self.request.user)
        c = Applicant.objects.get(created_by=self.request.user)
        return HttpResponseRedirect(reverse('requestform:app_complete', kwargs={'app_id': a.id}))


    def get_context_data(self, **kwargs):
        context = super(ApplicationVerify, self).get_context_data(**kwargs)
        context['app'] = Application.objects.get(id=self.kwargs['app_id'])
        context['answers'] = Answers.objects.get(created_by=self.request.user)
        context['applicant'] = Applicant.objects.get(created_by=self.request.user)
        return context

On Monday, September 1, 2014 4:15:39 PM UTC-4, James Schneider wrote:
It looks like you are passing the context an actual Answers object, not a Form or ModelForm object that will add/update an Answers object, hence the reason the trace back complains about a lack of 'fields'.

-James

On Monday, September 1, 2014, Brad Rice <brad...@gmail.com> wrote:
This document seems to indicate I can have two forms inside one set of form tags:


I cannot figure out how to set the context to be able to iterate over both forms.

{% crispy form %}

works on my model form.

If I try to set context in my view like this:

def get_context_data(self, **kwargs):
        context = super(ApplicationVerify, self).get_context_data(**kwargs)
        context['app'] = Application.objects.get(id=self.kwargs['app_id'])
        context['answers'] = Answers.objects.get(created_by=self.request.user)
        context['applicant'] = Applicant.objects.get(created_by=self.request.user)
        return context

and then use instead of crispy form

{% crispy answers %}

I get errors.

'Answers' object has no attribute 'fields'

How do I set a form to have context from several models?

I do see the context is coming into the template, it just is not treating them as having form widgets.

--
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.

James Schneider

unread,
Sep 1, 2014, 8:45:05 PM9/1/14
to django...@googlegroups.com
I would assume you want something akin to the first example here:


Once you have the form object built with the instance of your Answer object, just pass it along.

Since I've never needed more than one form on a page, there may be other gotchas, but you should be able to Google for those. I don't believe that you'll need to do anything special though. Keep in mind that only one of the forms will be submitted (assuming each has it's own set of <form> tags). If you need to combine them and submit info for both, you may need to roll a single custom form class that can handle all of the data, or possibly look into Django formsets.

-James

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Brad Rice

unread,
Sep 2, 2014, 9:41:04 AM9/2/14
to django...@googlegroups.com
I wrote an app that allows people to login to fill out an application. It has quite a few text areas that have to be filled out, so the idea was to allow them to fill out partial and come back and work on it and eventually submit it. The last stage before submit I pull all the data into a final form and allow them to submit it if they are satisfied. It set's required on the fields at that stage. There are three related db tables (models). So maybe, I'm thinking about this wrong. Instead of having two or three forms on a single page, maybe I need to built one from from the three related models at the end.

James Schneider

unread,
Sep 3, 2014, 2:33:15 AM9/3/14
to django...@googlegroups.com
You'll probably want to look at setting up a FormWizard to break up your workflow into steps (fill out some fields, click next to continue, etc.). Django handles the multi-step forms for you very nicely.


I find wizards to be much more pleasing rather than scrolling through large monolithic forms (which is entirely possible to do if the FormWizard doesn't meet your needs). As a bonus, I believe you can import ModelForms directly into each step of the workflow, so you may not need to write any fancy forms and tie things together manually.

It seems more complicated than it is, unless you are doing something super crazy. You may need to create one or two small forms to capture the information that your ModelForms will not handle, especially if you want to keep the state of the form across logins, etc. (ie, save it to finish filling out later). That will probably require a separate model (for example, a model called Application) that ties in the user, and any other models that are used by the wizard so that you can pass them in when the steps are rendered down the road (I haven't actually used that strategy, just brainstorming, YMMV).

You may also be able to combine the data collected via the FormWizard and dump that into a FormPreview, but again, just the rusty wheels turning in my head, YMMV.


-James



To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages