我正在为用户注册使用Django 1.8和python 3.5
进行注册我创建了一个用户(使用自定义模型扩展用户模型扩展AbstractUser)(即我想将自定义字段添加到默认的django用户表中,如bio,出生日期等)
当我尝试输入用户名和密码时,我在/ signup /
收到错误TypeError这是我的mainpage/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
这是我的signup/forms.py
from django import forms
from mainpage.models import User
from django import forms
from django.contrib.auth.forms import UserCreationForm
class allusers1(forms.Form):
username1=forms.CharField(label = "Username")
password1=forms.CharField(label="passwordwa",max_length=40)
class Meta:
model=User
fields=(
'username',
'password',
)
def save(self,commit=True):
user=super(allusers1,self).save(commit=False)
user.username=self.cleaned_data['username1']
user.password=self.cleaned_data['password1']
if commit:
user.save()
return user
这是signup/views.py
from django.shortcuts import render
from .forms import allusers1
def signup(request):
form1=allusers1(request.POST or None)
if form1.is_valid():
form1.save()
context = {
"form1": form1,
}
return render(request, "signup.html",context)
某些附加信息 我正在引用此博客来创建AbstractUser https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#abstractuser
回溯 环境:
Request Method: POST
Request URL: http://localhost:8000/signup/
Django Version: 1.8
Python Version: 3.5.4
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainpage',
'signup',
'login',
'currency',
'rest_framework',
'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\Users\vaibhav2\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vaibhav2\PycharmProjects\MajorProject\src\signup\views.py" in signup
13. form1.save()
File "C:\Users\vaibhav2\PycharmProjects\MajorProject\src\signup\forms.py" in save
26. user=super(allusers1,self).save(commit=False)
Exception Type: AttributeError at /signup/
Exception Value: 'super' object has no attribute 'save'
答案 0 :(得分:1)
在signup / forms.py
中变化:
user=super(User,self).save(commit=False)
要
user = super(allusers1, self).save(commit=False)
阅读一些有关编码风格的文章。与PEP-8和Django style一样。它可以帮助其他人(以及您未来的自己)阅读和理解您的代码。