Re: crispy forms two forms and context

2014-09-03 Thread James Schneider
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.

https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/

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.

https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-preview/

-James



On Tue, Sep 2, 2014 at 6:41 AM, Brad Rice  wrote:

> 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.
>
>
> On Monday, September 1, 2014 8:45:05 PM UTC-4, James Schneider wrote:
>
>> I would assume you want something akin to the first example here:
>>
>> https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
>>
>> 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 
>> 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
>>
>>
>> On Monday, September 1, 2014, Brad Rice  wrote:
>>
>>> 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  wrote:

> This 

Re: crispy forms two forms and context

2014-09-02 Thread Brad Rice
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.

On Monday, September 1, 2014 8:45:05 PM UTC-4, James Schneider wrote:
>
> I would assume you want something akin to the first example here:
>
> https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
>
> 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  
> 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
>
>
> On Monday, September 1, 2014, Brad Rice  
> wrote:
>
>> 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  wrote:
>>>
 This document seems to indicate I can have two forms inside one set of 
 form tags:

 http://django-crispy-forms.readthedocs.org/en/latest/
 crispy_tag_forms.html#rendering-several-forms-with-helpers

 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+unsubscr...@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.
 To view this 

Re: crispy forms two forms and context

2014-09-01 Thread James Schneider
I would assume you want something akin to the first example here:

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

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


On Monday, September 1, 2014, Brad Rice  wrote:

> 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  wrote:
>>
>>> This document seems to indicate I can have two forms inside one set of
>>> form tags:
>>>
>>> http://django-crispy-forms.readthedocs.org/en/latest/
>>> crispy_tag_forms.html#rendering-several-forms-with-helpers
>>>
>>> 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+unsubscr...@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.
>>> 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.
>>>
>>  --
> 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+unsubscr...@googlegroups.com
> 
> .
> To post to this group, send email to django-users@googlegroups.com
> .
> Visit this group at 

Re: crispy forms two forms and context

2014-09-01 Thread Brad Rice
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  
> wrote:
>
>> This document seems to indicate I can have two forms inside one set of 
>> form tags:
>>
>>
>> http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html#rendering-several-forms-with-helpers
>>
>> 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+unsubscr...@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.
>> 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.
>>
>

-- 
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+unsubscr...@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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ae25d932-a31b-4a23-aac3-4b9920fb710e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: crispy forms two forms and context

2014-09-01 Thread James Schneider
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  wrote:

> This document seems to indicate I can have two forms inside one set of
> form tags:
>
>
> http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html#rendering-several-forms-with-helpers
>
> 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+unsubscr...@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.
> 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.
>

-- 
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+unsubscr...@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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciV9EY2dxtBNk_or3iMp%3DtQuSG6HKnoDdY2XMtJu46VOaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


crispy forms two forms and context

2014-09-01 Thread Brad Rice
This document seems to indicate I can have two forms inside one set of form 
tags:

http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html#rendering-several-forms-with-helpers

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+unsubscr...@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.
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.