1👍
✅
The login page should be accessed twice: when the user is redirected to it , so using GET, and again when the user sends the filled form, this time using POST. Your view logic should deal with the two types of requisition:
def user_login(request):
if request.POST:
form = UserForm(request.POST) # note that I renamed your form
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('profile', kwargs={username=user})
else: # request is GET
form = UserForm()
return render(request, 'login.html', {'form': form})
@require_GET()
def profile(request, username):
user = Users.objects.get(username=username)
interests = Interests.objects.filter(member=user).values_list('item', flat=True)
info = Bios.objects.get(author=user)
context = {
'user': user,
"interests": interests,
'info': info
}
return render(request, 'profile.html', context)
I also changed the profile view a little bit to use what I believe to be the best practices in Django function based views.
Source:stackexchange.com