'AnonymousUser' Object Has No Attribute '_meta'
views.py code from django.shortcuts import render,redirect from django.http import HttpResponse from django.contrib.auth.forms import UserCreationForm from tutorial import views fr
Solution 1:
It's better to use login_required
decorator. It will check whether user logged in or not without coding it explicitly.
from django.contrib.auth.decorators import login_required
@login_required
def edit_profile(request):
if request.method=="POST":
form = EditProfileForm(request.POST,instance=request.user)
if form.is_valid():
form.save()
return redirect('/account/profile')
else:
form = EditProfileForm(instance=request.user)
args = {'form' : form}
return render(request,'accounts/edit_profile.html',args)
Solution 2:
You are not logged in and trying to modify profile. To prevent such kind of error, add is_authenticated
validation to your view. If user is not authenticated, you can redirect him to login
page with redirect
shortcut function:
def edit_profile(request):
if request.user.is_authenticated:
if request.method=="POST":
form = EditProfileForm(request.POST,instance=request.user)
if form.is_valid():
form.save()
return redirect('/account/profile')
else:
form = EditProfileForm(instance=request.user)
args = {'form' : form}
return render(request,'accounts/edit_profile.html',args)
else:
return redirect('/login/')
Post a Comment for "'AnonymousUser' Object Has No Attribute '_meta'"