Skip to content

Commit e7f0e8d

Browse files
Taking data using Model Form Django
1 parent 6eede3a commit e7f0e8d

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

junction/profiles/admin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib import admin
2+
from .models import Profile
3+
4+
5+
class ProfileAdmin(admin.ModelAdmin):
6+
list_display = ('__unicode__', 'city', 'contact_no')
7+
search_fields = ('contact_no', 'city')
8+
9+
admin.site.register(Profile, ProfileAdmin)

junction/profiles/forms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django import forms
2+
from .models import Profile
3+
4+
5+
class ProfileForm(forms.ModelForm):
6+
class Meta:
7+
model = Profile
8+
fields = ['city', 'contact_no']
9+
exclude = ('user',)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('profiles', '0004_auto_20160709_2023'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='profile',
16+
name='city',
17+
field=models.CharField(max_length=100, null=True, blank=True),
18+
preserve_default=True,
19+
),
20+
migrations.AlterField(
21+
model_name='profile',
22+
name='contact_no',
23+
field=models.CharField(max_length=15, null=True, blank=True),
24+
preserve_default=True,
25+
),
26+
]

junction/profiles/views.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# Profile Stuff
1414
from .models import Profile
15+
from .forms import ProfileForm
1516

1617

1718
@login_required
@@ -30,11 +31,15 @@ def dashboard(request):
3031

3132
@login_required
3233
def profile(request):
34+
form = ProfileForm()
35+
user = request.user
3336
if request.method == "POST":
34-
city = request.POST.get('city')
35-
contact_no = request.POST.get('contact_no')
36-
Profile.objects.create(user=request.user, city=city, contact_no=contact_no)
37-
return HttpResponseRedirect("/profiles")
38-
37+
form = ProfileForm(request.POST)
38+
if form.is_valid():
39+
form = form.save(commit=False)
40+
form.user = user
41+
form.save()
42+
print "form saved"
43+
return HttpResponseRedirect("/profiles")
3944
elif request.method == "GET":
4045
return render(request, 'profiles/userprofile.html')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('proposals', '0020_auto_20160806_2023'),
11+
]
12+
13+
operations = [
14+
migrations.AlterModelOptions(
15+
name='proposalcomment',
16+
options={'ordering': ('created_at',)},
17+
),
18+
]

0 commit comments

Comments
 (0)