您好我的django用户身份验证问题,因为我使用的是AbstractBaseUser
模型
from Apps.grupo.models import Grupo
from Apps.empresa_area.models import Empresa_area
from Apps.archivos.models import Archivos
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser,
BaseUserManager,
Group,
)
class UserManager(BaseUserManager):
def create_user(self, email, password=None, is_staff=False, is_admin=False):
if not email:
raise ValueError("Usuuario tiene email")
if not password:
raise ValueError("usuario debe tener contraseña")
user_obj = self.model(
email =self.normalize_email(email)
)
user_obj.set_password(password)
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self, email, password=None):
user = self.create_user(
email,
password=password,
is_staff=True
)
return user
def create_superuser(self, email, password=None):
user = self.create_user(
email,
password=password,
is_staff=True,
is_admin=True,
)
return user
class User(AbstractBaseUser):
email = models.EmailField(max_length=200, unique=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
celular = models.CharField(max_length=20)
saludo = models.CharField(max_length=10)
grupo = models.ForeignKey(Grupo, null=True, blank=True, on_delete=models.CASCADE)
archivo = models.ForeignKey(Archivos, null=True, blank=True, on_delete=models.CASCADE)
fec_creacion = models.DateTimeField(auto_now=False, auto_now_add=False)
empresa_area_id = models.ForeignKey(Empresa_area, null=True, blank=True, on_delete=models.CASCADE)
cargo = models.ManyToManyField(Group)
USERNAME_FIELD = 'email'
REQUIRED_FILES = []
objects = UserManager()
class Meta:
verbose_name = ('user')
verbose_name_plural = ('users')
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
在此模型中授予通过电子邮件验证我的权限,我也在此视图中生成用户身份验证
视图
from django.contrib.auth import (
get_user_model,
authenticate,
login,
logout,
)
from django.views.generic import CreateView, FormView
from django.utils.http import is_safe_url
from django.shortcuts import render, redirect
from .forms import Login
User = get_user_model()
class LoginView(FormView):
form_class = Login
succes_url = '/'
template_name = 'Login.html'
def form_valid(self, form):
request = self.request
next_post = request.POST.get('next')
next_ = request.GET.get('next')
password = form.cleaned_data.get("password")
email = form.cleaned_data.get("email")
user = authenticate(request, username=email, password=password)
if user is not None:
login(request, user)
try:
del request.session['email']
except:
pass
return super(LoginView, self).form_invalid()
它正在验证,但它产生以下错误
form_invalid()缺少1个必需的位置参数:'form'
答案 0 :(得分:1)
您需要将form
参数传递给超级类调用form_invalid
,即
return super(LoginView, self).form_invalid(form)