所以我在Django中为一个学校项目尝试自定义用户模型。我创建了一些继承自AbstractUser类的自定义字段,编写了一个自定义UserManager并在管理页面中注册了模型,最后从一个表单渲染了一个模板,该表单扩展了UserCreationForm并使用我的CustomUser作为模型(这一切似乎都很好,所有字段都在管理中可见,在添加和细节视图中也是可见的)。我也可以添加用户直接从管理页面,这是工作正常。
然而,当我提交表单时,我被重定向并在终端中获得[26/Jul/2023 13:10:40] "POST /register/ HTTP/1.1" 302 0
,但用户没有保存在数据库中。
下面你可以找到我的文件。
models.py
import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import gettext_lazy as _
class CustomUserManager(BaseUserManager):
def create_user(self, email, password, first_name, last_name, birth_date, credit_card, **extra_fields):
email = self.normalize_email(email)
user = self.model(email=email, first_name=first_name, last_name=last_name, birth_date=birth_date, credit_card=credit_card)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True)
if extra_fields.get("is_staff") is not True:
raise ValueError(_("Superuser must have is_staff=True."))
if extra_fields.get("is_superuser") is not True:
raise ValueError(_("Superuser must have is_superuser=True."))
return self.create_user(email, password, **extra_fields)
class CustomUser(AbstractUser):
birth_date = models.DateField(default=datetime.date.today)
credit_card = models.IntegerField(default=0)
objects = CustomUserManager()
字符串
forms.py
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser
from django import forms
class RegisterUserForm(UserCreationForm):
email = forms.EmailField()
first_name = forms.CharField()
last_name = forms.CharField
birth_date = forms.DateField()
credit_card = forms.IntegerField()
class Meta:
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email', 'birth_date', 'credit_card', 'password1', 'password2')
型
admin.py
from django.contrib import admin
from .models import CustomUser
from django.contrib.auth.admin import UserAdmin
# Register your models here.
class CustomUserAdmin(UserAdmin):
list_display = (
'username', 'email', 'first_name', 'last_name', 'is_staff',
'birth_date', 'credit_card',
)
fieldsets = (
(None, {
'fields': ('username', 'password')
}),
('Personal info', {
'fields': ('first_name', 'last_name', 'email')
}),
('Permissions', {
'fields': (
'is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions'
)
}),
('Important dates', {
'fields': ('last_login', 'date_joined')
}),
('Additional info', {
'fields': ('birth_date', 'credit_card')
})
)
add_fieldsets = (
(None, {
'fields': ('username', 'password1', 'password2')
}),
('Personal info', {
'fields': ('first_name', 'last_name', 'email')
}),
('Permissions', {
'fields': (
'is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions'
)
}),
('Important dates', {
'fields': ('last_login', 'date_joined')
}),
('Additional info', {
'fields': ('birth_date', 'credit_card')
})
)
admin.site.register(CustomUser, CustomUserAdmin)
型
views.py
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from .forms import RegisterUserForm
from django.http import HttpResponse
def register_user(request):
form = RegisterUserForm
if request.POST:
form = RegisterUserForm(request.POST)
if form.is_valid():
form.save()
return redirect(test)
context = {'form':form}
return render(request, 'accounts/index.html', context)
def test(request):
return HttpResponse('TEST!')
型
index.html
<h3>Register User</Form></h3>
<hr>
<form action="" method="post">
{% csrf_token %}
{{form}}
<input type="submit">
</form>
型
在settings.py中,我包含了AUTH_USER_MODEL = 'accounts.CustomUser'
提交表单后,不会发生错误,但用户不会存储到数据库中。我们非常感谢您的帮助。
提前感谢!
1条答案
按热度按时间pvcm50d11#
我很酷,删除所有你包含在表单中的字段,因为你是CustomUser继承自AbstractUser,所以你有
from django.contrib.auth.forms import UserCreationForm from .models import CustomUser from django import forms
类RegisterUserForm(forms.ModelForm):Meta类:model = CustomUser字段=('username ','first_name ','last_name ',' email ','