1👍
✅
You can try this :
def profile_page(request, username):
if request.user.is_authenticated():
if request.user.username == username:
# removed user since already in request.user and available in template as 'user'
# removed taskitems since directly available in template as 'user.taskitem_set.all'
return render(request, 'profile.html')
else:
return HttpResponseRedirect(reverse('profile_page', args=(request.user.username,)))
else:
return render(request, 'login.html')
0👍
Remove the username
parameter since each user should only view their own profile. You can also use the login_required
decorator to remove the extra conditional:
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def profile_page(request):
return render(request, 'profile.html')
Make sure to set LOGIN_URL
in your settings.py
so unauthenticated users get redirected to the right spot.
- [Answer]-What is the equivalent of South's "schemamigration –update" for Django>=1.7?
- [Answer]-Django; Why empty_label is not shown in ModelChoiceField?
- [Answer]-Why do we need fields value in Meta class in Django forms?
Source:stackexchange.com