Unable to authenticate user from oracle database (python 3.8.3 and django 3.0)

158 views Asked by At

how do I a login in from the database? I need to use two elements (user and password) of a table from Bd to perform a login. Someone know the format of what i need to put in models.py, views.py or more specific, the things that i need to do a correct login, thanks.

2

There are 2 answers

0
Utkarsh Kaushik On

Try using django's inbuilt auth functionalities to authenticate logins.

from django.contrib.auth import authenticate, login

def loginView(request):
    if request.method == 'POST':
        user = authenticate(username = request.POST['username'], password = request.POST['password'])
        if user is not None: # means a user exists in the system
            login(request, user) #login function to log user in.
            return redirect('/')
    if request.user.is_authenticated: # checks if user is already logged in or not
        return redirect('/')

If you want to register new users using "User.objects.create(email,password)"

Refer - auth

0
alireza khorami On

Django provides authentication ,i recommend checking out the doc .

in the models.py section of your application you can import the built-in User Class in order to have access to login.

use this at the top of your models.py

from django.contrib.auth.models import User
class Post(models.Model):
...
...
    author = models.ForeignKey(User)

now you can Create users in the command line for example using:

>>> User.objects.create(username='prozesh', email='[email protected]', password='**********')

you should also use authenticate and login functions to do your job in the views.py section but i strongly recommend reading the doc or this article