我承认我是 Django 的新手,但我一整天都在为这个问题而头撞墙。我创建了一个新的应用程序并从 django_registration 继承了类,但似乎我的表单数据从未保存过。我的字段显示并可在前端和管理区域访问,但用户注册似乎从未发生,并且我没有收到错误。 (它与标准 django 注册一起工作得很好,但我想扩展它并为未来做好计划。)
表格.py
from django_registration.forms import RegistrationForm
from authentication.models import MyCustomUser
from django import forms
class MyCustomUserForm(RegistrationForm):
class Meta(RegistrationForm.Meta):
model = MyCustomUser
#fields = "__all__"
fields = ['username', 'email', 'first_name', 'last_name', 'profile_photo', 'password1', 'password2']
模型.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django import forms
class MyCustomUser(AbstractUser):
ROLE_CHOICES = (
('APPLICANT', 'Applicant'),
('EMPLOYEE', 'Employee'),
('ADMIN', 'Admin'),
(None, 'None')
)
role = models.CharField(max_length=30, choices=ROLE_CHOICES)
profile_photo = models.ImageField()
#username = forms.CharField(max_length = 100) #this is already handled for us in AbstractUser
#email = forms.EmailField(max_length = 100) #this is already handled for us in AbstractUser
#password1 = forms.CharField(widget = forms.PasswordInput(), max_length = 100) #this is already handled for us in AbstractUser
#password2 = forms.CharField(widget = forms.PasswordInput(), max_length = 100) #this is already handled for us in AbstractUser
first = forms.CharField(max_length = 100 )
last = forms.CharField(max_length = 100)
#USERNAME_FIELD = username #this is already handled for us in AbstractUser
REQUIRED_FIELDS = []
管理员.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import MyCustomUserForm
from .models import MyCustomUser
class CustomUserAdmin(UserAdmin):
add_form = MyCustomUserForm
#form = CustomUserChangeForm
model = MyCustomUser
list_display = ["email", "username", "first_name", "last_name", "profile_photo", "role"]
admin.site.register(MyCustomUser, CustomUserAdmin)
设置.py
AUTH_USER_MODEL = 'authentication.MyCustomUser'
url.py
path('accounts/register/', SignUpView.as_view(), name='django_registration_register')
views.py
from django.shortcuts import render, redirect
from django.views.generic import View
from django.views import generic
from django_registration.views import RegistrationView as BaseRegistrationView
from authentication.forms import MyCustomUserForm
class SignUpView(BaseRegistrationView):#generic.CreateView
form_class = MyCustomUserForm
template_name = "authentication/registration_form.html"
我尝试将https://github.com/ubernostrum/django-registration/blob/trunk/src/django_registration/backends/activation/views.py中的方法添加到我的views.py中,但没有区别。我想如果我继承了这个类,那么方法无论如何都是可以访问的。我已经浏览了https://django-registration.readthedocs.io/en/latest/index.html的文档,试图弄清楚我需要做什么,但我不清楚我在这里缺少什么。
您的
MyCustomUser
模型继承自 AbstractUser
,这是正确的,但直接在模型中使用 Django 表单字段(first
和 last
变量)是不正确的。
模型应该定义数据库字段,而不是表单字段。您应该删除这些并使用 AbstractUser 提供的 first_name
和 last_name
字段,如下所示:
from django.contrib.auth.models import AbstractUser
from django.db import models
class MyCustomUser(AbstractUser):
ROLE_CHOICES = (
('APPLICANT', 'Applicant'),
('EMPLOYEE', 'Employee'),
('ADMIN', 'Admin'),
(None, 'None')
)
role = models.CharField(max_length=30, choices=ROLE_CHOICES, default=None, blank=True, null=True)
profile_photo = models.ImageField(upload_to='profile_photos/', blank=True, null=True) # Specify upload directory and make it optional if you want
这应该可以解决您的问题。