0

I'm Prety new in Django. After a few google search, I find full CRUD and I know how to handle that. But in User registration, I fell some problem all over the online every one uses Form.py to handle registration form but I don't want to use Form.py I like to customize it.

but the problem is when I use auth for login then Django auth says it's a wrong password

I use

authenticate(email=email,password=password)

for login check

Is anyone please explain custom login registration without using Form.py with some example.

Here is my View.py Code

def loginCheck(request):
    if request.method == 'POST':
        username = request.POST.get('username'),
        password = request.POST.get('password'),
        user = authenticate(request, username=username, password=password)

        if user is not None:

            return HttpResponse('Find User')
        else:
            return HttpResponse("Not Find User")

and my User Registration Code

def registration(request):
    checkData = AuthUser.objects.filter(email=request.POST.get('email'))
    if not checkData:
        User.objects.create_user(
            username=request.POST.get('username'),
            email=request.POST.get('email'),
            password=(request.POST.get('password')),
        )
        messages.add_message(request, messages.INFO, 'User Saved Successfully')
        return redirect('loginView')
    else:
        messages.add_message(request, messages.INFO, 'Email Already Exists')
        return redirect('loginView')

My Login code return Not Find User.

Antu
  • 2,197
  • 3
  • 25
  • 40

2 Answers2

1

If you want to your own registration process, you must use set_password function for saving password.

from django.contrib.auth.models import User

user = User.objects.get_or_create(username='john')
user.set_password('new password')
user.save()
Ali
  • 2,541
  • 2
  • 17
  • 31
  • Thank you for your answer but how do I authenticate password for login? `auth.authenticate(email=email,password=password)` is this ok for this? – Antu Aug 05 '18 at 07:15
  • After django receive password, it must hash that first, then store it on db. so we must use set_password. then you can use auth.authenticate(username=username,password=password) – Ali Aug 05 '18 at 07:27
  • but with username, not email. if you want to use email, you must customize User model – Ali Aug 05 '18 at 07:28
1

Try this minimal example. In this, you can create User and log in through API.

import json
from django.views.generic import View
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.contrib.auth import authenticate, login


class UserRegisterView(View):
    def get(self, request):
        return JsonResponse({"message": "METHOD NOT ALLOWED"})

    def post(self, request, *args, **kwargs):
        json_body = json.loads(request.body)
        username = json_body.get('username')
        password = json_body.get('password')
        email = json_body.get('email')
        is_staff = json_body.get('is_staff', False)
        is_superuser = json_body.get('is_superuser', False)
        User.objects.create_user(
            username=username, email=email,
            password=password, is_staff=is_staff,
            is_superuser=is_superuser
        )
        return JsonResponse({"message": "User created"})


class LoginView(View):
    def get(self, request):
        return JsonResponse({"message": "METHOD NOT ALLOWED"})

    def post(self, request, *args, **kwargs):
        '''
        input data format:

        {
        "username" : "my username",
        "password":"mysecret123@"
        }

        '''
        json_body = json.loads(request.body)
        username = json_body.get('username')
        password = json_body.get('password')
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return JsonResponse({"message": "login success"})
        else:
            return JsonResponse({"message": "login failed"})



Why I parsed request.body ?
How to receive json data using HTTP POST request in DJANGO

Reference:
How to authenticate user in DJANGO (official-doc)

UPDAYE-1
updated view as per the request,checking sufficent data in POST method (bold code)

def loginCheck(request):
    if request.method == 'POST' and 'username' in request.POST and 'password' in request.POST:
        username = request.POST.get('username'),
        password = request.POST.get('password'),
        user = authenticate(request, username=username, password=password)

        if user is not None:

            return HttpResponse('Find User')
        else:
            return HttpResponse("Not Find User")
    return HttpResponse("POST data wrong")
JPG
  • 82,442
  • 19
  • 127
  • 206
  • @Antu I updated the answer with few references, check those too , if you want – JPG Aug 05 '18 at 07:45
  • Hi @jerin I edit my question would you please check this? – Antu Aug 05 '18 at 10:02
  • 1
    try to print `username` and `password` before authenticate. CHeck those values – JPG Aug 05 '18 at 11:52
  • anyway, I updated the answer. Please look into bolded text – JPG Aug 05 '18 at 11:58
  • I check username and password before **authenticate** its return data no problem with that but when I try to authenticate and find user `user = authenticate(request, username=username, password=password)` it's getting None, I also try to find the only username it's not getting that. Sir would you please run my code? I'm really in a serious problem with this. – Antu Aug 06 '18 at 03:54
  • Can you share that project? – JPG Aug 06 '18 at 03:56
  • Sure, Sir, it's just a simple login registration system before starting my main project I'm working on it. how can I share with you this? – Antu Aug 06 '18 at 03:58
  • github or something similar...dont forget to add `requirements.txt` file – JPG Aug 06 '18 at 04:05
  • 1
    remove ending comma in `username = request.POST.get('username'),` and `password = request.POST.get('password'),` . – JPG Aug 06 '18 at 04:34
  • 1
    If you put comma, python consider it as `tupple` rather than string – JPG Aug 06 '18 at 04:34
  • Thank You so much sir I understand what is wrong with my code thank you – Antu Aug 06 '18 at 04:40
  • look at this [line](https://github.com/Antu7/TestRepo/blob/master/login/views.py#L33), remove the comma from line 33 and 34 – JPG Aug 06 '18 at 04:42