views:

168

answers:

2

I'm attempting to take 4-5 fields from a large django form and display them on the thanks page.

I want to disply the values with a good degree of control, as i'll be building an iFrame with parameterd querystrings based on the form inputs.

Currently I have:

forms.py ----

-*- encoding: utf-8 -*-
from django import forms
from django.forms import extras, ModelForm
from django.utils.safestring import mark_safe
from siteapp.compare.models import Compare

HOWMUCH_CHOICES = (
    ('', '--------------------------'),
    ('20000', '20,000'),
    ('30000', '30,000'),
...
    ('2000000', '2,000,000'),
)

HOWLONG_CHOICES = (
    ('', '--------------------------'),
    ('1', '1 Year'),
...
    ('39', '39 Years'),
    ('40', '40 Years'),
)

...etc.

class ComparisonForm(forms.Form):
    js = mark_safe(u"document.compareForm.how_much.selectedindex = 4;")
    how_much = forms.ChoiceField(choices=HOWMUCH_CHOICES,
       widget=forms.Select(attrs={'onMouseOver':'setDefaults()','class':'required validate-selection','title':'Select value of cover required','onLoad': js}))
    how_long = forms.ChoiceField(choices=HOWLONG_CHOICES,
       widget=forms.Select(attrs={'class':'required validate-selection','title':'Select length of cover required'}))
    who_for = forms.ChoiceField(choices=WHOFOR_CHOICES,
       widget=forms.Select(attrs={'class':'required validate-selection','title':'Select whether you require cover for a partner also'}))
        ...
    class Meta:
     model = Compare

models.py -----

class Compare(models.Model):
    how_much = models.CharField(max_length=28,choices=HOWMUCH_CHOICES#,default='100000'
    )
    how_long = models.CharField(max_length=28,choices=HOWLONG_CHOICES)
    who_for = models.CharField(max_length=28,choices=WHOFOR_CHOICES)
...
    partner_date_of_birth = models.DateField(blank=True)

def __unicode__(self):
 return self.name

views.py----

def qc_comparison(request):
    return render_to_response('compare/compare.html', locals(), context_instance=RequestContext(request))

urls.py----

(r'^compare/thanks/$', 'django.views.generic.simple.direct_to_template', {'template': 'compare/thanks.html'}),

I'm trying to dig out the best way to do this fromt he documentation, but it's not clear how to pass the variables correctly to a thanks page. Any help appreciated!

A: 

The values available to the template are provided by the view.

The render_to_response function provides a dictionary of values that are passed to the template. See this.

For no good reason, you've provided locals(). Not sure why.

You want to provide a dictionary like request.POST -- not locals().


Your locals() will have request. That means your template can use request.POST to access the form.

S.Lott
Thansk for the response - am using locals() beacuse I have a custom context processor in play also (it identifies subdomains and makes their strings available to Django).Will try the alteration suggested and see how it goes.
Chris
That's working a treat. I'm passing the form data via a contextprocessor also. Here's the code for reference:<code>def compare_form(request): from siteapp.compare.forms import ComparisonForm if request.method == 'POST': form = ComparisonForm(request.POST) if form.is_valid(): cd = form.cleaned_data return {'cd': cd} else: form = ComparisonForm() return {'form': form}</code>Then calling the context in the template.
Chris
A: 

I'm not sure if I understand what you are trying to do.

But if you want to render form values in plain text you can try django-renderformplain. Just initalize your form with POST (or GET) data as you would in any other form processing view, and pass your form instance in the context.

muhuk