Login

Reusable form template with generic view

Author:
roldandvg
Posted:
December 2, 2015
Language:
HTML/template
Version:
1.7
Score:
0 (after 0 ratings)

If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution.

Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# model.py
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    gender = models.CharField(max_length=1, choices=[('M', 'Male'),('F','Female')])
    birthdate = models.DateField()

# forms.py
from django.forms import ModelForm, TextInput, ChoiceField, Select
from myapp.models import Person # change myapp for you app

class PersonForm(ModelForm):
    gender = ChoiceFields(
        label='gender',
        choices=[('M', 'Male'),('F','Female')],
        widget=Select(attrs={'class': 'form-control'})
    )

    class Meta:
        model = Person
        fields = '__all__'
        widgets = {
            'first_name': TextInput(attrs={'class': 'form-control'}),
            'last_name': TextInput(attrs={'class': 'form-control'}),
            'birthdate': TextInput(attrs={'class': 'form-control'})
        }

# generic-form.html

<form role="form" class="form" method="{% block form_method %}post{% endblock %}" enctype="multipart/form-data">
    {% csrf_token %}
    {# Only add visible fields #}
    {% for field in form.visible_fields %}
        <div class="col-md-2">
            <div class="form-group">
                <label class="control-label" for="{{ field.auto_id }}">{{ field.label }}</label>
                {{ field }}
                {% if field.errors %}
                    <div class="control-label" for="inputError">
                        {{ field.errors.as_text }}
                    </div>
                {% endif %}
            </div>
        </div>
    {% endfor %}
    {% for field in form.hidden_fields %}{{field}}{% endfor %}
    <div class="col-md-12">
        <button type="submit" class="btn btn-success">submit</button>
    </div>
</form>

# view.py
from django.core.urlresolvers import reverse_lazy
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView
from myapp.models import Person
from myapp.forms import PersonForm

class PersonCreate(SuccessMessageMixin, CreateView):
    model = Person
    form_class = PersonForm
    template = 'generic-form.html'
    success_url = reverse_lazy('url_to_success') # Change for you url success
    success_message = 'message_to_success' # Change for you message success

More like this

  1. Bootstrap Accordian by Netplay4 5 years, 10 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  4. Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago
  5. django form template with bootstrap by youyongsong 9 years, 10 months ago

Comments

Please login first before commenting.