views:

24

answers:

1

Hi Guys,

I am trying to access data.get_age_display in my email template. I can't seem to get the display of this. I am not sure what I am doing wrong, I've using get_FIELD_display numerous times before but passed as context to a normal template. Is there something different with forms?

class RequestForm(forms.Form):

    ADULT = 1
    SENIOR = 2
    STUDENT = 3

    AGE_GROUP = (
        (ADULT, 'Adult'),
        (SENIOR, 'Senior'),
        (STUDENT, 'Student'),
    )


    name = forms.CharField(max_length=255)
    phone = forms.CharField(max_length=15)
    age = forms.ChoiceField(choices=AGE_GROUP)
    details = forms.CharField(widget=forms.Textarea())

    def save(self):
        order = Order(
            name = self.cleaned_data['name'],
            phone = self.cleaned_data['phone'],
            age = self.cleaned_data['age'],
            details = self.cleaned_data['details'],
        )
        order.save()

        template = loader.get_template('request_email.txt')

        # send over the order object in an email extracted so they can handle the ticket order
        context = Context({
            'data': order,
        })

        #import pdb; pdb.set_trace()

        email_subject = 'Request Tickets'
        mail_managers(email_subject, template.render(context))

in my request_email.txt all I am doing is {{ data.get_age_display }} any ideas?

Jeff

A: 

You haven't shown the code for the Order model that you're creating. Are you sure that the age field on the model has choices set?

Any reason you're not using a ModelForm? You're creating an Order object within the form's save() method, but not returning it. A modelform would do that for you, as well as removing the need to redeclare the fields for the form.

Daniel Roseman
Daniel, I omitted some code for simplicity, modelform would not be good for what I am doing. Here is the code for the order model
Jeffrey
class Order(models.Model): ADULT = 1 SENIOR = 2 STUDENT = 3 AGE_GROUP = ( (ADULT, 'Adult'), (SENIOR, 'Senior'), (STUDENT, 'Student'), ) name = models.CharField(max_length=255) phone = models.CharField(max_length=15) age = models.IntegerField(choices=AGE_GROUP)
Jeffrey