我一直在尝试使用电子邮件和将 custom_user应用写入注册 / 登录 >密码即可。
我遵循了一个教程,但我必须从我的代码中留下一些东西,因为我能够注册一个新用户(它已创建,我在管理面板中检查过),但我只能使用在具有“manage.py createsuperuser”功能的终端。
我不知道我在注册码或登录代码上是否犯了错误 无论如何,这是我的 models.py
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
now = datetime.now()
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=254, unique=True, null=True)
first_name = models.CharField(max_length=254, blank=True)
second_name = models.CharField(max_length=254, blank=True)
email = models.EmailField(blank=True, unique=True)
address1 = models.CharField(max_length=254, blank=True)
address2 = models.CharField(max_length=254, blank=True)
area_code = models.CharField(max_length=20, blank=True)
country_code = models.CharField(max_length=10, blank=True)
date_joined = models.DateTimeField(_('date joined'), default=datetime.now())
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username',]
objects = CustomUserManager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
这是 forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from models import CustomUser, CustomUserManager
class CustomUserCreationForm(UserCreationForm):
"""
A form that creates a user, with no privileges, from the given email and password.
"""
def __init__(self, *args, **kwargs):
super(CustomUserCreationForm, self).__init__(*args, **kwargs)
# del self.fields['username']
class Meta:
model = CustomUser
fields = ("email", "first_name", "second_name",)
class CustomUserChangeForm(UserChangeForm):
"""
A form for updating users. Includes all the fields on the user, but replaces the password field with admin's password hash display field.
"""
def __init__(self, *args, **kwargs):
super(CustomUserChangeForm, self).__init__(*args, **kwargs)
# del self.fields['username']
class Meta:
model = CustomUser
fields = ("email", "first_name", "second_name",)
这是 views.py 文件
def register(request):
registered = False
if request.method == 'POST':
user_form = CustomUserCreationForm(request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
return redirect('../')
else:
print user_form.errors
else:
user_form = CustomUserCreationForm()
return render(request, 'register.html', {'user_form': user_form, 'registered': registered})
def login(request):
c = {}
c.update(csrf(request))
return render(request, 'login.html', c)
def auth_view(request):
if request.method == 'POST':
username = request.POST.get('email', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
auth.login(request, user)
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/login')
我能做些什么吗?
谢谢!