Skip to content

Commit 76f525e

Browse files
committed
add MultiForm demo
1 parent fe0eb6c commit 76f525e

File tree

7 files changed

+43
-21
lines changed

7 files changed

+43
-21
lines changed

demo/base/forms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ class RecordForm(forms.ModelForm):
1515
class Meta(object):
1616
model = Record
1717
fields = ['title', 'description']
18+
19+
20+
class ContactForm(forms.Form):
21+
subject = forms.CharField(max_length=100)
22+
message = forms.CharField(widget=forms.Textarea)
23+
24+
25+
class UserForm(forms.Form):
26+
sender_email = forms.EmailField()

demo/base/templates/base.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
<h1>
1212
<a href="{% url 'index' %}">Multi Form View</a>
1313
</h1>
14-
<nav>
15-
<ul>
16-
<li><a href="{% url 'records' %}">MultiModelForm Example</a></li>
17-
</ul>
18-
</nav>
1914
<hr/>
2015
</header>
2116
<main>

demo/base/templates/contact.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "base.html" %}
2+
3+
4+
{% block content %}
5+
<form method="post">
6+
{% csrf_token %}
7+
{{ user_form.as_p }}
8+
{{ contact_form.as_p }}
9+
<input type="submit" value="submit"/>
10+
</form>
11+
{% endblock %}

demo/base/templates/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
{% block content %}
55
<ul>
6-
<li><a href="{% url 'records' %}">MultiForm Example</a></li>
7-
<li><a href="">MultiModelForm Example</a></li>
8-
</{% endblock %}
6+
<li><a href="{% url 'records' %}">MultiModelForm Example</a></li>
7+
<li><a href="{% url 'contact' %}">MultiForm Example</a></li>
8+
</ul>
9+
{% endblock %}

demo/base/urls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
from django.conf import settings
22
from django.conf.urls import url
33
from django.conf.urls.static import static
4+
from django.views.generic import TemplateView
45

56
from base import views
67

78

89
urlpatterns = [
9-
url(r'^$', views.IndexView.as_view(), name='index'),
10+
url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
1011

1112
url(r'^records/$', views.RecordListView.as_view(), name='records'),
1213
url(r'^records/new/$', views.RecordFormView.as_view(), name='records_new'),
1314
url(r'^records/edit/(?P<record_id>\d+)$', views.RecordFormView.as_view(), name='records_edit'),
15+
16+
url(r'^contact/$', views.ContactView.as_view(), name='contact'),
17+
1418
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

demo/base/views.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
from django.core.urlresolvers import reverse
2-
from django.http import HttpResponseRedirect
3-
from django.views.generic import TemplateView
42
from django.views.generic.list import ListView
53

6-
from multi_form_view import MultiModelFormView
4+
from multi_form_view import MultiFormView, MultiModelFormView
75

8-
from base.forms import PhotoForm, RecordForm
6+
from base.forms import ContactForm, PhotoForm, RecordForm, UserForm
97
from base.models import Photo, Record
108

119

12-
class IndexView(TemplateView):
13-
template_name = "base.html"
14-
15-
1610
class RecordListView(ListView):
1711
template_name = 'records.html'
1812
model = Record
@@ -21,7 +15,7 @@ class RecordListView(ListView):
2115
class RecordFormView(MultiModelFormView):
2216
form_classes = {
2317
'photo_form' : PhotoForm,
24-
'record_form' : RecordForm
18+
'record_form' : RecordForm,
2519
}
2620
record_id=None
2721
template_name = 'records_form.html'
@@ -40,9 +34,17 @@ def get_objects(self):
4034
def get_success_url(self):
4135
return reverse('records')
4236

43-
def forms_valid(self, forms):
37+
def on_forms_valid(self, forms):
4438
photo = forms['photo_form'].save()
4539
record = forms['record_form'].save(commit=False)
4640
record.photo = photo
4741
record.save()
48-
return HttpResponseRedirect(self.get_success_url())
42+
43+
44+
class ContactView(MultiFormView):
45+
form_classes = {
46+
'contact_form' : ContactForm,
47+
'user_form' : UserForm,
48+
}
49+
record_id=None
50+
template_name = 'contact.html'

demo/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Django==1.9.8
2-
django-multi-form-view==0.0.1
2+
django-multi-form-view==0.0.2
33
Pillow==3.3.0
44
behave==1.2.5
55
behave-django==0.3.0

0 commit comments

Comments
 (0)