I am new to the community. I usually am able to just find my answer, but I have been searching and searching, and can't seem to find anything similar to what I've got going on. I have a ModelForm set up to register new users. I got it to work, and send information over to the sqlite database. However, when I try to log the newly registered user in it says:
"Please enter a correct username and password. Note that both fields may be case-sensitive."
login.html:
{% extends 'app/register.html' %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login">
</form>
<a href="{% url 'index' %}">Back to Homepage</a>
<br>
<a href="{% url 'password_reset' %}">Reset password</a>
<br>
<a href="{% url 'register' %}">Register</a>
{% endblock %}
views.py
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
from datetime import datetime
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.urls import reverse
from . import forms
from django.contrib import auth
def index(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
}
)
def register(request):
if request.method == "GET":
form = forms.Registration(request.GET)
return render(
request, "app/register.html",
{"form": form}
)
elif request.method == "POST":
form = forms.Registration(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect(reverse("index"))
models.py
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Patient(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
#birthdate = models.DateField(auto_now=False, null=True, blank=True)
gender = models.CharField(max_length=100, blank=True)
allergies = models.TextField(blank=True)
email = models.CharField(max_length=200)
username = models.CharField(max_length=100)
password = models.CharField(max_length=100)
last_login = models.CharField(max_length=100)
and forms.py:
from django import forms
from . import models
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
class Registration(forms.ModelForm):
class Meta:
model = models.Patient
fields = ('first_name','last_name', 'gender', 'email', 'username', 'password', 'allergies',)
any help would be greatly appreciated. Please forgive me if I've left something out, just let me know. Thank you!
So after a lot of trial and error and fiddling, I ultimately fixed it this way, thanks to Abdul Aziz Barkat's suggestion above.
I changed Patient(models.Model) to Patient(AbstractUser)
And then I also had to change the Registration(forms.ModelForm) to Registration(UserCreationForm). All of the users are authenticating just fine now, thank you!