python - Django 1.10错误, 'NoneType'对象不可调用

标签 python django python-2.7

我正在尝试为 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 的应用中,注册 View 如下所示:

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']

...最后 View 使用的模板包含:

<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),错误消息 TypeError at/sign_up/'NoneType' object不可调用显示而不是user_created View 。

回溯:

File "/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(request)

File "/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "/Users/carltiden/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/carltiden/GitHub/a_project/a_project/sign_up/views.py" in index 24. form.cleaned_data['date_of_birth'],

File "/Users/carltiden/GitHub/a_project/a_project/custom_user/models.py" in create_user 15. date_of_birth=date_of_birth

Exception Type: TypeError at /sign_up/ Exception Value: 'NoneType' object is not callable

(第 15 行是实际代码中的 date_of_birth=date_of_birth 行。)为什么会发生这种情况?我在这里做错了什么?

最佳答案

这根本不是你使用管理器的方式。您永远不会在模型定义之外自己实例化它们;您可以通过模型本身访问它们。

    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'],
        )

关于python - Django 1.10错误, 'NoneType'对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41663310/

相关文章:

python - 如何保存 Tensorflow 估计器模型以在 Google ML Engine 上提供服务

python - python 上下文与变量作用域有何关系?

python - django app_details() 收到意外的关键字参数 'slug' 错误

python - 实例化 python2 类并在另一个模块中使用的正确方法是什么

python - 使用 Python 发送 Telegram 键盘

python-2.7 - 访问模板文件 Wagtail 中的子页面模型属性

javascript - 如何向服务器端提交一个javascript变量

python - Selenium Desired Capabilities - 为 PhantomJS 驱动程序设置 handlesAlerts

python - Gunicorn 没有重新加载 Django 应用程序

当 DEBUG 为 False 时,Django flatpages 会引发 404(存在 404 和 500 模板)