Skip to content

Commit 651c368

Browse files
Added Views for user Profile
1 parent 8db1f43 commit 651c368

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

junction/profiles/views.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,28 @@ def profile(request):
3737
detail = None
3838

3939
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'))
40+
user = User.objects.get(pk=username.id)
41+
detail = Profile.objects.filter(user=user).exists()
42+
if detail:
43+
detail = Profile.objects.get(user=user)
44+
detail_form = ProfileForm(request.POST, instance=detail)
45+
if detail_form.is_valid():
46+
detail = detail_form.save()
47+
return HttpResponseRedirect(reverse('profiles:dashboard'))
48+
else:
49+
user = User.objects.get(pk=username.id)
50+
detail_form = ProfileForm(request.POST)
51+
if detail_form.is_valid():
52+
detail_form = detail_form.save(commit=False)
53+
detail_form.user = user
54+
detail_form.save()
55+
return HttpResponseRedirect(reverse('profiles:dashboard'))
4756

4857
elif request.method == "GET":
4958
user = User.objects.get(pk=username.id)
5059
detail = Profile.objects.filter(user=user).exists()
5160
if detail:
61+
detail = Profile.objects.get(user=user)
5262
return render(request, 'profiles/userprofile.html', {'detail': detail})
5363
else:
5464
return render(request, 'profiles/userprofile.html')

0 commit comments

Comments
 (0)