Reputation: 60
I'm trying to Make an auth system without using the Django user.authenticate system because I want to make 2 seperate Login Systems. 1 For the IT & Web Development Dept. and another 1 for the general users. So I'm using the same MySql Database and the Auth_user for my IT dept. and I created a accounts app which I want to use the accounts_benutzer (users in german) table.
I can add thanks to the app system users etc. over the Admin Panel but I want that the users which are registered by an IT Dept User log in on the Main Page with the given username and password
accounts/models.py:
class Benutzer(models.Model):
regex = RegexValidator(regex=r'^\+?1?\d{7,15}$')
name = models.CharField(max_length=100)
passwort = models.CharField(max_length=32)
email = models.EmailField(max_length=254)
....
accounts/views.py :
from django.shortcuts import redirect, render
from django.contrib import messages
from django.contrib.auth.models import User, auth
from .models import Benutzer
def login(request):
if request.method == 'POST':
username = request.POST['name']
passwort = request.POST['passwort']
if (username == Benutzer.name) and (passwort == Benutzer.passwort):
return redirect('/')
else:
messages.info(request,'Falsche Zugangs Daten!')
return redirect('/accounts/login')
else:
return render(request, 'login.html')
login.html:
{% load static %}
<form action="login" method="post">
{% csrf_token %}
<p>Benutzername: <input type="text" name="name" placeholder="Benutzer Name"></p>
<p>Passwort: <input type="password" name="passwort" placeholder="Passwort"></p>
<input type="submit" value="Bestaetigen">-<input type="reset" value="Löschen">
</form>
<div>
{% for message in messages %}
<h3>{{message}}</h3>
{% endfor %}
I know it doesn't make sense to use Benutzer.user or Benutzer.passwort but thats the point which I am asking. How Can I manage to compare the data which I get from the website with the data on my MySQL table accounts_benutzer to give the user access?
Upvotes: 0
Views: 743
Reputation: 146
I think there are other ways, but this should work.
def login(request):
if request.method == 'POST':
username = request.POST['name']
passwort = request.POST['passwort']
B_user = Benutzer.objects.filter(name=username).first()
if B_user and B_user.passwort == passwort:
return redirect('/')
else:
messages.info(request,'Falsche Zugangs Daten!')
return redirect('/accounts/login')
else:
return render(request, 'login.html')
Upvotes: 1