0

I have register username and password using the following code

views.py

def registerPage(request):
    if request.method == 'POST':
        form = AdminUserForm(request.POST)
        Password = request.POST['Password'] 
        if form.is_valid():
            user = form.save(commit=False)
            user.Password = make_password(Password)
            user.save()
            messages.success(request, "user created succesfully!!! login now")
            return redirect('login')
    else:
        form = AdminUserForm()
    return render(request, 'signup.html', {'form': form})

Can someone tell me how to verify username and password while login, and logout?

models.py

class AdminUser(models.Model):
    Id=models.AutoField(primary_key=True)
    Name=models.CharField(max_length=100, null=False, blank=False)
    Username=models.CharField(max_length=100, null=False, blank=False)
    Password=models.CharField(max_length=100, null=False, blank=False)

    class Meta:
        db_table='admin_users'

forms.py

class AdminUserForm(forms.ModelForm):
    class Meta:
        model= AdminUser
        fields = '__all__'

thanks in advance, please help in login and logout

divine
  • 87
  • 10
  • Is this `AdminUser` model being substituted for the built-in `auth.User` model? If you want to use integrate with the functionality provided by `django.contrib.auth` you'll need to change your model to align with what is expected https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model – Iain Shelvington Jun 25 '21 at 05:43
  • Actually, I have to create a user model which is not related to the Django auth model, as I want a different table than auth_user – divine Jun 25 '21 at 05:47
  • 1
    Django's auth_user is extremely powerful when it comes to authentication, authorization, and it handles most of the stuff for you automatically. The common approach is to extend the auth_user by adding your additional fields. Please read some posts about extending django's user and it'll be much easier for you. https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django – Cagatay Barin Jun 25 '21 at 06:40
  • normally (when you don't use any framework) you would have to encrypt password from Form and compare with encrypted version in database. But Django may have it somewhere in its code. – furas Jun 25 '21 at 07:21

1 Answers1

0

I have tried to solve this in this way. here I have added a get_user_by_username function in model.py. I have edited my code to add this function. If someone knows any other method, please post it here.

In views.py

def loginPage(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        adminuser =AdminUser.get_user_by_username(username)

        if adminuser:
            check=check_password(password, adminuser.Password)
            # correct username and password login the user
            if check:
                messages.info(request, "You Have Successfully Logged in!!")
                return redirect('Home')
            else:
                messages.error(request, "Invalid username or password.")
                return redirect('login')

    return render(request, 'login.html')

in models.py

class AdminUser(models.Model):
    Id=models.AutoField(primary_key=True)
    Name=models.CharField(max_length=100, null=False, blank=False)
    Username=models.CharField(max_length=100, null=False, blank=False, unique=True, error_messages ={"unique":"Username should be unique"})
    Password=models.CharField(max_length=100, null=False, blank=False)
    class Meta:
        db_table='admin_users'

    @staticmethod
    def get_user_by_username(Username):
        try:
            return AdminUser.objects.get(Username=Username)
        except:
            return False
divine
  • 87
  • 10