Hi everyone,
I decided to rewrite my basic “Singup/Signin” app in order to understand more about Django.
I wrote my own forms with my own check password function etc.
Thus, after the user is authenticated, the login function doesn’t work, do you have an idea why ?
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager
class UserManager(BaseUserManager):
def get_by_natural_key(self, username):
try:
user = User.objects.get(email=username)
print("GET BY NATURAL KEY => " + str(user))
except:
return None
else:
return user
def get_by_id(self, id):
try:
user = User.objects.get(pk=id)
print("GET BY ID => " + str(user))
except:
return None
else:
return user
class User(AbstractUser):
username = None
email = models.EmailField(max_length=40, unique=True, blank=False)
password = models.CharField(max_length=40, blank=False)
objects = UserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
def __str__(self):
return self.email
signin function in views.py
def signinUser(request):
if request.method == "POST":
form = signinUserForm(request.POST)
if form.is_valid():
user = authenticate(request,
username=form.cleaned_data["email"],
password=form.cleaned_data["password"],)
print("USER IS => " + str(user))
if user is not None:
login(request, user)
return redirect("app_base:index")
else:
form.add_error(None,"ERROR : Incorrect email or password")
else:
form.add_error(None,"ERROR : Invalid form please retry")
else:
form = signinUserForm()
return render(request, "app_accounts/signin.html", {"form":form})
signin.html
{% block page_content %}
<div id="page_navbar">
<a href="{% url 'app_accounts:signupUser' %}">Signup</a>
{% if not request.user.is_authenticated %}
<a href="{% url 'app_accounts:signinUser' %}">Signin</a>
{% else %}
<a href="{% url 'app_accounts:logoutUser' %}">Logout</a>
{% endif %}
</div>
{% endblock %}
My output is :
[28/Nov/2020 10:39:55] "GET /favicon.ico HTTP/1.1" 404 2349
[28/Nov/2020 10:39:56] "GET /accounts/signin HTTP/1.1" 200 647
USER IS => test@mail.com
[28/Nov/2020 10:40:01] "POST /accounts/signin HTTP/1.1" 302 0
[28/Nov/2020 10:40:01] "GET / HTTP/1.1" 200 340
So according to my output, the user is authenticated, I’m redirected to the index view (home page) indeed but the “Signout” menu is not shown whereas I configured the index.html…