Skip to content

Commit e3fdec1

Browse files
committed
add example for multiple of the same forms
1 parent 9edbf21 commit e3fdec1

File tree

13 files changed

+195
-22
lines changed

13 files changed

+195
-22
lines changed

demo/base/forms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django import forms
22

3-
from base.models import Photo, Record
3+
from base.models import Photo, Profile, Record
44

55

66
class PhotoForm(forms.ModelForm):
@@ -16,6 +16,12 @@ class Meta(object):
1616
model = Record
1717
fields = ['title', 'description']
1818

19+
class ProfileForm(forms.ModelForm):
20+
21+
class Meta(object):
22+
model = Profile
23+
fields = ['name']
24+
1925

2026
class ContactForm(forms.Form):
2127
subject = forms.CharField(max_length=100)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.8 on 2017-09-14 07:29
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('base', '0001_initial'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Profile',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('name', models.CharField(max_length=100)),
21+
('created_at', models.DateTimeField(auto_now_add=True)),
22+
('updated_at', models.DateTimeField(auto_now=True)),
23+
],
24+
),
25+
migrations.AlterField(
26+
model_name='photo',
27+
name='tag',
28+
field=models.CharField(choices=[(b'A', b'Animal'), (b'P', b'Plant'), (b'O', b'Other'), (b'U', b'Unknown')], default=b'U', max_length=1),
29+
),
30+
migrations.AlterField(
31+
model_name='record',
32+
name='photo',
33+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='base.Photo'),
34+
),
35+
migrations.AddField(
36+
model_name='profile',
37+
name='avatar',
38+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profile_avatars', to='base.Photo'),
39+
),
40+
migrations.AddField(
41+
model_name='profile',
42+
name='background',
43+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profile_backgrounds', to='base.Photo'),
44+
),
45+
]

demo/base/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ class Record(models.Model):
2727

2828
created_at = models.DateTimeField(auto_now_add=True)
2929
updated_at = models.DateTimeField(auto_now=True)
30+
31+
32+
class Profile(models.Model):
33+
avatar = models.ForeignKey(Photo, related_name="profile_avatars")
34+
background = models.ForeignKey(Photo, related_name="profile_backgrounds")
35+
name = models.CharField(max_length=100)
36+
37+
created_at = models.DateTimeField(auto_now_add=True)
38+
updated_at = models.DateTimeField(auto_now=True)

demo/base/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{% block content %}
55
<ul>
66
<li><a href="{% url 'records' %}">MultiModelForm Example</a></li>
7+
<li><a href="{% url 'profiles' %}">MultiModelForm Example with 2 of the same form types</a></li>
78
<li><a href="{% url 'contact' %}">MultiForm Example</a></li>
89
</ul>
910
{% endblock %}

demo/base/templates/profiles.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% extends "base.html" %}
2+
3+
4+
{% block content %}
5+
<a href="{% url 'profiles_new' %}">Create Profile</a>
6+
<table>
7+
<tr>
8+
<th>Name</th>
9+
<th>Avatar</th>
10+
<th>Background</th>
11+
<th></th>
12+
</tr>
13+
{% for profile in object_list %}
14+
<tr>
15+
<td>{{ profile.name }}</td>
16+
<td>
17+
<img src="{{ profile.avatar.image.url }}" height="50" width="50" alt="profile avatar"/>
18+
{{ profile.avatar.tag }}
19+
</td>
20+
<td>
21+
<img
22+
src="{{ profile.background.image.url }}"
23+
height="50"
24+
width="50"
25+
alt="profile background image"
26+
/>
27+
{{ profile.background.tag }}
28+
</td>
29+
<td>
30+
<a href="{% url 'profiles_edit' profile.id %}">Edit</a>
31+
</td>
32+
</tr>
33+
{% endfor %}
34+
</table>
35+
{% endblock %}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "base.html" %}
2+
3+
4+
{% block content %}
5+
<form method="post" enctype="multipart/form-data">
6+
{% csrf_token %}
7+
8+
{{ forms.profile_form.as_p }}
9+
<label>Avatar</label>
10+
{{ forms.avatar_form.as_p }}
11+
<label>Background photo</label>
12+
{{ forms.background_form.as_p }}
13+
<input type="submit" value="submit"/>
14+
</form>
15+
{% endblock %}

demo/base/urls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
url(r'^records/new/$', views.RecordFormView.as_view(), name='records_new'),
1414
url(r'^records/edit/(?P<record_id>\d+)$', views.RecordFormView.as_view(), name='records_edit'),
1515

16+
url(r'^profiles/$', views.ProfileListView.as_view(), name='profiles'),
17+
url(r'^profiles/new/$', views.ProfileFormView.as_view(), name='profiles_new'),
18+
url(r'^profiles/edit/(?P<record_id>\d+)$', views.ProfileFormView.as_view(), name='profiles_edit'),
19+
1620
url(r'^contact/$', views.ContactView.as_view(), name='contact'),
17-
url(r'^cntact/sent/$', TemplateView.as_view(template_name='contact-sent.html'), name='contact_sent'),
21+
url(r'^cntact/sent/$', TemplateView.as_view(template_name='contact_sent.html'), name='contact_sent'),
1822

1923
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

demo/base/views/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from contact import ContactView
2+
from profile import ProfileListView, ProfileFormView
3+
from record import RecordListView, RecordFormView

demo/base/views/contact.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.core.urlresolvers import reverse
2+
3+
from multi_form_view import MultiFormView
4+
5+
from base.forms import ContactForm, UserForm
6+
7+
8+
class ContactView(MultiFormView):
9+
form_classes = {
10+
'contact_form' : ContactForm,
11+
'user_form' : UserForm,
12+
}
13+
record_id = None
14+
template_name = 'contact.html'
15+
16+
def get_success_url(self):
17+
return reverse('contact_sent')

0 commit comments

Comments
 (0)