0
I’m trying to log into an account of Django to redirect to the main page of my project, but the command login
returns me the following error.
AttributeError at /login/
'AnonymousUser' object has no attribute '_meta'
I am using Onetoonefield to use cnpj/password from a table in my database.
I am testing both with the Django login and password as the table and both return the same error.
py views.
def login_log(request):
form = PessoaForm(request.POST, None)
if request.method == 'POST':
cnpj = form.data['cnpj']
senha = form.data['senha']
print(form.is_valid())
if form.is_valid():
user = authenticate(request, cnpj=cnpj, senha=senha)
user2 = authenticate(request, username=cnpj, password=senha)
print(user)
print(user2)
print(login(request, user))
#return redirect('dashboard', idpessoa)
return render(request, 'login.html', {'form': form})
idpessoa
is used to send next to the url and access the user who is logging in. In my code it has the utility correctly and is working. (Just to awaken awareness that it is not the mistake)
I know the mistake is on this line
print(login(request, user))
and the print
is there only for testing
In case you need me Forms.py
class PessoaForm(ModelForm): class Meta: model = User fields = ['cnpj', 'senha']
Thank you again for describing a complete and instructive response! I can’t test anymore because I don’t know if I can and I want to go back to that error, but I believe that if I play your code will work, and if not, there’s still another post where the other bugs are fixed.
– Nicolas