JDavies
JDavies

Reputation: 2770

Rendering and customising a django form in the template

So I've managed to output my form to the template. But i'm having a little trouble with one of the forms as the frontend styling has already been done. I'd just like to be able to output the label names etc within in the HTML that already exists. I can get the form the output to the template using the following:

<form method="post">
    {% csrf_token %}
        {{enquiry_form.as_ul}}                          
            <button type="submit" class="submit softcolor">
                <span>Submit</span>
            </button>
</form>

However, I have a ManytoMany field on the model where I would like users to be able to tick multiple options with a checkbox. When i output the above I get a select box as with a list of the categories it's pulling through.

{% csrf_token %}
    {% for fields in enquiry_form %}
        <div class="block-365 center clearfix">
            <label>{{fields.label}}</label>
            <div class="input-line">
                <input name="">
            </div>
        </div>
    {% endfor %}

So i can do something like the above, which works fine. But for some reason the CSRF protection stops working. I also need to be able to be able to access the items in the Brochure model(The manyToMany field).

Here the code I have so far:

forms.py

from django.forms import ModelForm
from django import forms

from contact_enquiries import models

class ContactEnquiryForm(ModelForm):

    class Meta:
        model = models.ContactForm
        fields=('name','email','company', 'subject', 'message')


class BrochureRequestForm(ModelForm):

    class Meta:
        model = models.ContactForm
        fields = ('name', 'email', 'company', 'address', 'brochure',)

views.py

from contact_enquiries import forms
from django.shortcuts import render
from django.http import HttpResponseRedirect


def contact(request):
    if request.method == 'POST':
        form = forms.ContactEnquiryForm(request.POST)
        en_form = forms.BrochureRequestForm(request.POST)

        print(request.POST)
        if form.is_valid():
            #print "Form looks good!"
            #e = models.ContactEnquiry(form.cleaned_data)
            #e.save()
            form.save()
            return HttpResponseRedirect('/thanks/')
        else:
            form = forms.ContactEnquiryForm()
            en_form = forms.BrochureRequestForm()

    return render(request, 'contact.html', {
            'contact_form' : form,
            'enquiry_form' : en_form,
            })

models.py

class ContactForm(models.Model):
    name = models.CharField(max_length="300")

    company = models.CharField(max_length="200")

    email = models.CharField(max_length="300")

    subject = models.TextField()

    message = models.TextField()

    address = models.TextField()

    brochure = models.ManyToManyField("Brochure", blank=True)

    def __unicode__(self):
        self.name

    class Meta:
        verbose_name = "Contact Enquiry"
        verbose_name_plural = "Contact Enquiries"


class Brochure(models.Model):        
    title = models.CharField(max_length="200")

    def __unicode__(self):
        self.title

    class Meta:
        verbose_name = "Brochure"
        verbose_name_plural = "Brochures"

And the HTML is displayed above. Not sure which the best approach is when outputting the form to the template, but i need to be able to use the HTML/CSS suplied to save me time re-doing it. Also I need to able to get the checkbox working on the Brochure form. As the idea is when they fill in the form they can choose which PDF they would like to be sent.

Any questions please ask!

Upvotes: 1

Views: 1500

Answers (2)

JDavies
JDavies

Reputation: 2770

Ok so it turns out I could just render the template like so:

                       {% csrf_token %}
                        {% for fields in enquiry_form %}
                        <div class="block-365 center clearfix">
                            <label>{{fields.label}}</label>
                            <div class="input-line">
                                <input name="">
                            </div>
                        </div>
                        {% endfor %}

Then in my class where I set the fields to output to the template I can set a widget to the field like so:

fields = ('name', 'email', 'company', 'address', 'brochure',)

widgets = {'brochure': forms.CheckboxSelectMultiple()}

This is will then render the ManytoManyField as checkboxes.

Upvotes: 1

goromlagche
goromlagche

Reputation: 442

django forms are excellent. For checkboxes you can use this django checkboxes

Upvotes: 0

Related Questions