Create a custom User model and Authenticate with it

102 views
Skip to first unread message

Devansh Soni

unread,
Mar 3, 2021, 11:55:15 AM3/3/21
to Django users
Hi, 

I'm currently working on a Django project in which I have to create a User model with fields such as username, email, college, branch, semester, password, etc. And use this model to authenticate a user with an email and password.

How can I achieve this? Or what are the ways to achieve this?

Thank you

Kasper Laudrup

unread,
Mar 3, 2021, 12:03:15 PM3/3/21
to django...@googlegroups.com

Chelsea Fan

unread,
Mar 3, 2021, 12:08:32 PM3/3/21
to django...@googlegroups.com
try this code, do not forget import authenticate (from django.contrib.auth import authenticate, login)
modify this code

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6206bd8c-dec4-1362-75a4-34d370e1d5ea%40stacktrace.dk.
Capture.PNG

Gabriel Araya Garcia

unread,
Mar 3, 2021, 10:56:36 PM3/3/21
to django...@googlegroups.com
Davansh:
The tables are made already in Admin Django, you only have to build template (html), and the view. For example, in views.py:

def login_ini(request):
    variable1 = 'Pantalla de Acceso al Sistema'
    error_log = 'ok'
    username = request.POST.get('username')
    password = request.POST.get('password') # valor del template
    user = auth.authenticate(username=username, password=password)
    if request.method == "POST":
        if user is not None and user.is_active:
            #Correct password, and the user is marked "active"
            auth.login(request, user)
            request.session['username_x'] = username # variable gobal
            request.session['id_x'] = user.id   # variable gobal
            return HttpResponseRedirect("principal")
            error_log = "error"

    context = {'user':user,"variable1":variable1,"error_log":error_log,}

    return render(request,'login_ini.html',context)
    

def log_out(request):
logout(request)
return redirect('login_ini')

Where login_ini.html, is the authentication template.

Only you must to build authentication template


Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages