i’m struggling a bit with this problem, some says i have to override AuthenticationForm but i don’t know how cause i got a custom user registration and login, i’ll post it:
urls.py
urlpatterns = [
path('login/', LoginView.as_view(), {'template_name': 'accounts/login.html'}, name='login'),
Note im using default loginview right now.
So in views.py i must have this
def login(request):
return render(request, '/login.html')
...
...
Of course login.html is just like this
<div class='container'>
<h1>Welcome</h1>
<p>
you can login here!
</p>
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type='submit'>Login</button>
</form>
</div>
In models.py my custom user is this one:
class ProfiloUtente(models.Model):
user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
utente = models.ForeignKey(Utente, on_delete=models.CASCADE)
studio = models.ManyToManyField(Studio)
telefono_fisso = models.CharField(max_length=20, default=None, blank=True)
telefono_mobile = models.CharField(max_length=20, default=None, blank=True)
indirizzo = models.CharField(max_length=40, default=None, blank=True)
citta = models.CharField(max_length=50, default=None, blank=True)
cap = models.CharField(max_length=5, default=None, blank=True)
provincia = models.CharField(max_length=30, default=None, blank=True)
cod_fiscale = models.CharField(max_length=16, default=None, blank=True)
p_iva = models.CharField(max_length=27, default=None, blank=True)
def __str__(self):
return self.user.username
And the fields i use from The default django user are these:
forms.py
class RegistrationForm(UserCreationForm):
"""docstring for RegistrationForm"""
email = forms.EmailField(required=True)
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=100, required=True)
class Meta: # define a metadata related to this class
model = User
fields = (
'username',
'email',
'first_name',
'last_name',
'password1',
'password2',
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save() # running sql in database to store data
return user
could someone help me figure out how could i manage to authenticate this user by using Email or Username?
Every help is appreciated!
Thanks in advance