0

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

My views/models/template is like:

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?

2
  • What exactly do you mean by "2 seperate Login Systems"? I can't see any reason why django's built in authentication system wouldn't work for staff & general users. Your database connection information should live in settings.py and enable you to use the database. Commented Sep 16, 2020 at 21:21
  • 1) Educational experience 2) Doing something out of the box 3) Giving the End-User the choice of seperate Staff & General Users ( Make a better feel when the end-user can put himself/herself in other categories)(Just a capitalist thought tho) And my database connection works I can put inside new stuff over the Administration site . I just want to make a authorization without the build in because I want a 2nd log in for normal users and I don't want the normal user database mixed with my Admin user Database. Commented Sep 17, 2020 at 16:29

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate this was actually very simple and works for this situation. I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.