猿问

在 Django 中创建自定义模型的登录

有很多方法可以在Django中实现用户身份验证。我使用模型表单来创建表单和注册过程。现在我真的不知道登录。我不能使用内置登录,因为我的模型(它被称为)和django的内置用户模型之间没有关系。我想得到一些关于方式的建议。这是我的代码:Account


views.py:


def sign_up(request):

    context = {}

    form = SignUpForm(request.POST or None, request.FILES or None)


    if form.is_valid():

        form.save()

        return redirect('/')

    else:

        form = SignUpForm(request.POST or None, request.FILES or None)

    context['form'] = form

    return render(request, "sign_up.html", context)

models.py:


class Account(models.Model):

    first_name = models.CharField(max_length=30)

    last_name = models.CharField(max_length=30)

    email = models.EmailField(max_length=60)

    username = models.CharField(max_length=30)

    password = models.CharField(max_length=60)

    university_name = models.ForeignKey(

        University, on_delete=models.CASCADE, blank=True, null=True)

    isPhysicalAccount = models.BooleanField(default=False)


    def __str__(self):

        return self.username

forms.py:


lass SignUpForm(forms.ModelForm):

    class Meta:

        model = Account

        fields = "__all__"


    def __init__(self, *args, **kwargs):

        super(SignUpForm, self).__init__(*args, **kwargs)


慕村9548890
浏览 81回答 2
2回答

小唯快跑啊

扩展 django 用户模型:from django.contrib.auth.models import AbstractUserclass User(AbstractUser):    university_name = models.ForeignKey(        University, on_delete=models.CASCADE, blank=True, null=True)    isPhysicalAccount = models.BooleanField(default=False)    class Meta:        db_table = 'auth_user'        ordering = ('id', 'username', 'first_name', 'last_name')settings.py:    AUTH_USER_MODEL = 'appname.User'要获取使用此模型的用户:from django.contrib.auth import get_user_modeluser_model = get_user_model()user_model.objects.get(id=1)使用您的逻辑注册视图:from django.contrib.auth.forms import AuthenticationFormdef sign_up(request):    context = {}    form = AuthenticationForm(request.POST or None, request.FILES or None)    if form.is_valid():        form.save()        return redirect('/')    else:        form = AuthenticationForm(request.POST or None, request.FILES or None)    context['form'] = form    return render(request, "sign_up.html", context)

陪伴而非守候

举例说明如何进行自定义Accountfrom django.contrib.auth.base_user import AbstractBaseUserfrom django.contrib.auth.models import ( PermissionsMixin,   _user_has_module_perms, _user_has_perm )class Account(AbstractBaseUser, PermissionsMixin): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField(max_length=60) username = models.CharField(max_length=30) password = models.CharField(max_length=60) university_name = models.ForeignKey(    University, on_delete=models.CASCADE, blank=True, null=True) isPhysicalAccount = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) def __str__(self):  return self.username def has_perm(self, perm, obj=None):    if self.is_active and self.is_superuser:        return True    return _user_has_perm(self, perm, obj) def has_perms(self, perm_list, obj=None):    return all(self.has_perm(perm, obj) for perm in perm_list) def has_module_perms(self, app_label):    if self.is_active and self.is_superuser:        return True    return _user_has_module_perms(self, app_label)USERNAME_FIELD = 'username'REQUIRED_FIELDS = ['username']settings.pyAUTH_USER_MODEL = 'yourapp.Account'
随时随地看视频慕课网APP

相关分类

Python
我要回答