我正在尝试为 django 站点制作一个简单的用户注册表单。这里是货
模型.py
from django.db import models
from django import forms
from django.conf import settings
from taggit.managers import TaggableManager
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class UserManager(BaseUserManager):
def create_user(self, email, username_input, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
username=username_input,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(email,
password=password,
username=username,
)
user.is_admin = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
# Last login, is active, and password are included automatically
username = models.CharField(max_length=30, blank=False, unique=True, )
email = models.EmailField(max_length=150, blank=False)
join_date = models.DateTimeField(auto_now_add=True)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class UserForm(forms.Form):
email = forms.EmailField(max_length=150, required=True)
username = forms.CharField(max_length=30, required=True)
password1 = forms.CharField(label='Password', widget=forms.PasswordInput(), max_length=30, required=True)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(), max_length=30, required=True)
视图.py
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from myapp.models import *
def signup(request):
if request.method == 'POST': # If the form has been submitted...
if not request.POST['password1'] == request.POST['password2']:
return HttpResponse("HOLY JESUS BATMAN YOUR PASSWORDS DONT MATCH!")
form = UserForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
password = request.POST['password1']
username = request.POST['username']
email = request.POST['email']
manager = UserManager()
manager.create_user(
email,
username,
password,
)
return HttpResponse('Huzzah! Success!')
else:
form = UserForm() # An unbound form
return render(request, 'signup.html', {
'form': form,
})
痕迹
Environment:
Request Method: POST
Request URL: http://10.0.1.25:8000/signup/
Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'taggit',
'bild',
'bildloguser')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/bildlog/virtenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/myapp/myapp/views.py" in signup
19. password,
File "/home/user/myapp/myapp/models.py" in create_user
13. username=username_input,
Exception Type: TypeError at /signup/
Exception Value: 'NoneType' object is not callable
所以,我已经把头发拉了一段时间,尝试了各种各样的东西,查看了各种各样的教程,但我仍然遇到同样的错误。我知道很多,但我认为要关注的是 create_user 函数。任何帮助都会非常感激,因为这最终会让我发疯。
谢谢