Skip to content

Commit 2affb32

Browse files
Added profile view with update feature.
1 parent e7f0e8d commit 2affb32

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

junction/profiles/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class ProfileForm(forms.ModelForm):
66
class Meta:
77
model = Profile
88
fields = ['city', 'contact_no']
9-
exclude = ('user',)
9+
exclude = ['user',]

junction/profiles/views.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
# Third Party Stuff
55
from django.contrib.auth.decorators import login_required
6-
from django.shortcuts import render
6+
from django.shortcuts import render, get_object_or_404, redirect
77
from django.views.decorators.http import require_http_methods
88
from django.http import HttpResponseRedirect
9+
from django.core.urlresolvers import reverse
10+
from django.contrib.auth.models import User
911

1012
# Junction Stuff
1113
from junction.conferences.models import Conference
@@ -31,15 +33,22 @@ def dashboard(request):
3133

3234
@login_required
3335
def profile(request):
34-
form = ProfileForm()
35-
user = request.user
36-
if request.method == "POST":
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")
36+
username = request.user
37+
detail = None
38+
39+
if request.method == "POST" and username == request.user:
40+
user = get_object_or_404(User, pk=username.id)
41+
detail = get_object_or_404(Profile, user=user)
42+
detail_form = ProfileForm(request.POST, instance=detail)
43+
44+
if detail_form.is_valid():
45+
detail = detail_form.save()
46+
return HttpResponseRedirect(reverse('profiles:dashboard'))
47+
4448
elif request.method == "GET":
45-
return render(request, 'profiles/userprofile.html')
49+
user = get_object_or_404(User, pk=username.id)
50+
detail = get_object_or_404(Profile, user=user)
51+
if detail:
52+
return render(request, 'profiles/userprofile.html', {'detail':detail})
53+
else:
54+
return render(request, 'profiles/userprofile.html')

junction/templates/profiles/userprofile.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ <h2 class="text-center">Edit Profile</h2>
3636
<div class="form-group">
3737
<label for="city" class="col-lg-4 control-label"> City :</label>
3838
<div class="col-lg-8">
39-
<input autofocus="autofocus" name="city" placeholder="Enter Your City" type="text">
39+
<input autofocus="autofocus" name="city" placeholder="Enter Your City" type="text" value="{{detail.city}}">
4040
</div>
4141
</div>
4242
<div class="form-group">
4343
<label for="contact" class="col-lg-4 control-label"> Contact :</label>
4444
<div class="col-lg-8">
45-
<input autofocus="autofocus" name="contact_no" placeholder="Enter Your Contact No:" type="text" pattern="\d{10}">
45+
<input autofocus="autofocus" name="contact_no" placeholder="Enter Your Contact No:" type="text" pattern="\d{10}" value="{{detail.contact_no}}" >
4646
</div>
4747
</div>
4848
<div class="form-group">

0 commit comments

Comments
 (0)