I am building a web project with Django to facilitate distribution in case of an earthquake. The user needs to be a member as a Mukhtar or a Helper. The registration system works successfully. But the login part is constantly returning None value even though my login information is absolutely correct.
models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
from base64 import b32encode
from hashlib import sha1
from random import random
# Create random id
def pkgen():
rude = ('lol',)
bad_pk = True
while bad_pk:
pk = b32encode(sha1(str(random())).digest()).lower()[:24]
bad_pk = False
for rw in rude:
if pk.find(rw) >= 0: bad_pk = True
return pk
# Create your models here.
class User(AbstractUser):
muhtar = models.BooleanField(null=False,default=False)
tc = models.CharField(max_length=11, null=False, unique=True)
address = models.TextField(null=False)
need_help = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'tc' #Uyarı! Bu benzersiz olmasını ister
REQUIRED_FIELDS = ['username']
def __str__(self):
return f"{self.username}"
class sos(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
sos_key = models.CharField(max_length=24, primary_key=True, default=pkgen, unique=True)
address = models.TextField(null=False, unique=False)
def __str__(self):
return f"{self.user}"
views.py:
from django.shortcuts import render, redirect
from .models import User
from django.contrib.auth import authenticate, login, logout
# Create your views here.
def index(request):
return render(request, "harita.html")
def register(request):
if request.method == "POST":
username = request.POST.get("kullaniciadi")
email = request.POST.get("email")
tc = request.POST.get("tc-no")
password = request.POST.get("password")
muhtar = request.POST.get("muhtar-check")
if muhtar == "on":
muhtar = True
else:
muhtar = False
if username == "" or password == "" or tc == "" or email == "":
return render(request, "register.html", {"error": "LĂŒtfen tĂŒm alanları doldurun."})
#elif len(tc) != 11:
# return render(request, "register.html", {"error": "TC kimlik numarası 11 haneli olmalıdır."})
elif User.objects.filter(tc=tc).exists() or User.objects.filter(email=email).exists():
return render(request, "register.html", {"error": "Bu kimlik numarası veya e-posta adresi zaten kullanılıyor."})
else:
user=User.objects.create_user(username=username, email=email, password=password, tc=tc, muhtar=muhtar)
user.save()
return redirect("login")
return render(request, "register.html")
def login(request):
if request.method == "POST":
tc = request.POST.get("tc-no")
password = request.POST.get("password")
email = request.POST.get("email")
if tc == "" or password == "" or email == "":
return render(request, "login.html", {"error": "LĂŒtfen tĂŒm alanları doldurun."})
user = authenticate(request=request, tc=tc, password=password, email=email)
print(user)
if user is not None:
login(request, user)
return redirect("index")
else:
print(f"""
***************
TC: {tc}
Ćifre: {password}
E-posta: {email}
***************""")
return render(request, "login.html", {"error": "E-posta, T.C. veya Ćifre hatalı."})
return render(request, "login.html")
def logoutPage(request):
logout(request)
return render(request, 'logout.html')
def profile(request):
if request.user.is_authenticated == False:
return redirect("register")
else:
user = User.objects.get(tc=request.user.tc)
return render(request, "profile.html", {"user": user})
def sos(request):
if request.user.is_authenticated == False:
return redirect("register")
elif not request.user.muhtar:
return render(request, "harita.html", {"error": "Yardım çaÄırma yetkiniz yok."})
if request.method == "POST":
malzemeler = request.POST.get("exampleFormControlTextarea1")
if not malzemeler:
return render(request, "sos.html", {"fillError": "LĂŒtfen gerekli alanları doldurun."})
# Kullanıcının adresini ve usernamesini alabilirsiniz, örneÄin:
adres = request.user.address
username = request.user.username
# Ä°Ćlemlerinizi yapabilirsiniz, örneÄin bu bilgileri bir veritabanına kaydedebilirsiniz.
# BaĆarılı bir Ćekilde iĆlem yapıldıÄında kullanıcıya bir mesaj göndermek için
return render(request, "sos.html", {"succmsg": "Yardım çaÄrınız baĆarıyla gönderildi."})
return render(request, "sos.html")
login.html:
{% load static %}
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GiriĆ Sayfası</title>
<link rel="stylesheet" href="{% static 'libs/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'register_and_login.css' %}">
<script src="{% static 'libs/jquery.min.js' %}"></script>
</head>
<body>
{% block navbar %}
{% include "navbar.html" %}
{% endblock navbar %}
<div class="container">
<br>
<div id="brs">
<br><br><br><br>
</div>
<!--giriĆ formu-->
<form id="giris-form" method="POST">
{% csrf_token %}
{% if error %}
<div class="alert alert-danger">{{ error }}</div>
{% endif %}
<h3>GiriĆ Yap</h3>
<label for="email">Email</label>
<input type="text" id="email" name="email">
<label for="tc-no">T.C. kimlik numarası</label>
<input type="text" id="tc-no" name="tc-no">
<label for="parola">Parola</label>
<input type="password" id="parola" name="password">
<button type="submit" id="giris-yap">GiriĆ Yap</button>
</form>
</div>
</body>
</html>
Iâm in a really difficult situation. I need to finish the project urgently and I canât make any progress because of this problem.