Найти - Пользователи
Полная версия: авторизация по email
Начало » Django » авторизация по email
1
zlodiak
помогите пожалуйста решить проблему

я сделал систему регистрации. пользователь вводит имя, email и пароль. регистрация работает(в БД создаются соответствующие записи в таблице auth_user).

теперь я хотел бы сделать форму авторизации. в которую пользователь вводит email, password. но в моём коде ошибка. после ввода email, password авторизации не происходит.

models.py:

from django.db import models
from django.contrib.auth.models import User, UserManager
class UserProfile(User):
	CHOICES_gender = (
		('0', 'М', ),
		('1', 'Ж', ),
	)
			
	gender = models.CharField(
		max_length=10, 
		choices=CHOICES_gender, 
		blank=False,
	)
	phone = models.CharField(
		max_length=50, 
		blank=False,
	)
	
	objects = UserManager()

forms.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.forms import ModelForm
import re
from app_accounts.models import UserProfile
class registrationForm(UserCreationForm):	
	username = forms.CharField(
		label='Отображаемое имя',
		help_text='',
		max_length=50, 
		required=True,
	)	
	email = forms.EmailField(
		label='Email',
		help_text='',
		required=True,
	)
	
	password1 = forms.CharField(
		label='Пароль',
		help_text='',
		required=True,
		widget=forms.PasswordInput,
	)	
	
	password2 = forms.CharField(
		label='Подтверждение пароля',
		help_text='',
		required=True,
		widget=forms.PasswordInput,
	)	
	class Meta:
		model = UserProfile
		fields = (  
			'username',    
			'email',    
			'password1', 
			'password2',
		)
	def clean_password1(self):
		password1 = self.cleaned_data['password1']
		q_letters = len(password1)
		if q_letters < 6:
			raise forms.ValidationError("Пароль не может быть короче 6 символов.")		
		return password1	
		
	def clean_username(self):
		username = self.cleaned_data['username']
		q_letters = len(username)
		if q_letters < 3:
			raise forms.ValidationError("Логин не может быть короче 3 символов.")		
		return username		
class authenticationCustomForm(AuthenticationForm):
	email = forms.EmailField(
		label='Email',
		widget=forms.TextInput(),		
	)
	password = forms.CharField(
		label='Пароль', 
		widget=forms.PasswordInput(),
	)
ihor_ua
Ти неуважний.
Зміни
from django.contrib.auth.models import User

на
from django.contrib.auth.models import AbstractUser as User

zlodiak
ihor_ua
Ти неуважний.Зміни

спасибо, но после того как прописываю в model.py:
from django.contrib.auth.models import UserManager
from django.contrib.auth.models import AbstractUser as User

какой-то ад начинается в консоли:
CommandError: One or more models did not validate:
auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
app_accounts.userprofile: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
app_accounts.userprofile: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
den4ik
Базу надо обновить (синхронизировать).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB