我正在尝试为Django项目构建用户注册页面。
我有一个名为 custom_user 的应用,其中 models.py 文件包含以下内容(除了导入):
class CustomUserManager(BaseUserManager):
def create_user(self, identifier, password, email, full_name, date_of_birth):
user = self.model(
identifier=identifier,
email=self.normalize_email(email),
full_name=full_name,
date_of_birth=date_of_birth
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, identifier, password, email, full_name, date_of_birth):
# Almost same as previous method.
class CustomUser(AbstractBaseUser, PermissionsMixin):
objects = CustomUserManager()
USERNAME_FIELD = 'identifier'
identifier = models.CharField(max_length=40, unique=True)
email = models.EmailField(max_length=200, unique=True)
full_name = models.CharField(max_length=200)
date_of_birth = models.DateField()
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
REQUIRED_FIELDS = ['full_name', 'email', 'date_of_birth']
def get_full_name(self):
return self.full_name
def get_short_name(self):
return self.identifier
在另一个名为 sign_up 的应用中,注册视图显示为:
def index(request):
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
# Create the new user.
manager = CustomUserManager()
user = manager.create_user(
form.cleaned_data['identifier'],
form.cleaned_data['password'],
form.cleaned_data['email'],
form.cleaned_data['full_name'],
form.cleaned_data['date_of_birth'],
)
return HttpResponseRedirect(reverse('sign_up:user_created'))
else:
form = CreateUserForm()
return render(request, 'sign_up/index.html', {'form': form})
views.py 文件会导入CreateUserForm
类(位于 sign_up 目录的 forms.py 中):
from django.forms import ModelForm
from custom_user.models import CustomUser
class CreateUserForm(ModelForm):
class Meta:
model = CustomUser
fields = ['identifier', 'password', 'email', 'full_name', 'date_of_birth']
...最后,视图使用的模板包含:
<h1>Welcome to the signup page!</h1>
<form action="{% url 'sign_up:index' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Sign up" />
</form>
提交表单时(form.is_valid()
似乎返回True
),错误消息 / error_up /'NoneType'对象中的TypeError不可调用显示而不是 user_created 视图。
回溯:
文件 “/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/exception.py” 在内心 39. response = get_response(request)
文件 “/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py” 在_get_response中 187. response = self.process_exception_by_middleware(e,request)
文件 “/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py” 在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件“/Users/carltiden/GitHub/a_project/a_project/sign_up/views.py”in 指数 24. form.cleaned_data ['date_of_birth'],
文件 “/Users/carltiden/GitHub/a_project/a_project/custom_user/models.py”in 创建用户 15. date_of_birth = date_of_birth
异常类型:/ error_up / Exception值的TypeError:'NoneType' 对象不可调用
(第15行是实际代码中的date_of_birth=date_of_birth
行。)为什么会发生这种情况;我在这做错了什么?
答案 0 :(得分:2)
根本不是你如何使用经理。你永远不会在模型定义之外自己实例化它们;你可以通过模型本身访问它们。
if form.is_valid():
user = CustomUser.objects.create_user(
form.cleaned_data['identifier'],
form.cleaned_data['password'],
form.cleaned_data['email'],
form.cleaned_data['full_name'],
form.cleaned_data['date_of_birth'],
)