我在创建用户时使用 Django 创建用户和对象。但是有一个错误
__init__() got an unexpected keyword argument 'user'
register()
在 view.py 中调用函数时。功能是:
def register(request):
'''signup view'''
if request.method=="POST":
form=RegisterForm(request.POST)
if form.is_valid():
username=form.cleaned_data["username"]
email=form.cleaned_data["email"]
password=form.cleaned_data["password"]
user=User.objects.create_user(username, email, password)
user.save()
return HttpResponseRedirect('/keenhome/accounts/login/')
else:
form = RegisterForm()
return render_to_response("polls/register.html", {'form':form}, context_instance=RequestContext(request))
#This is used for reinputting if failed to register
else:
form = RegisterForm()
return render_to_response("polls/register.html", {'form':form}, context_instance=RequestContext(request))
对象类是:
class LivingRoom(models.Model):
'''Living Room object'''
user = models.OneToOneField(User)
def __init__(self, temp=65):
self.temp=temp
TURN_ON_OFF = (
('ON', 'On'),
('OFF', 'Off'),
)
TEMP = (
('HIGH', 'High'),
('MEDIUM', 'Medium'),
('LOW', 'Low'),
)
on_off = models.CharField(max_length=2, choices=TURN_ON_OFF)
temp = models.CharField(max_length=2, choices=TEMP)
#signal function: if a user is created, add control livingroom to the user
def create_control_livingroom(sender, instance, created, **kwargs):
if created:
LivingRoom.objects.create(user=instance)
post_save.connect(create_control_livingroom, sender=User)
Django 错误页面通知错误信息:
user=User.objects.create_user(username, email, password)
和
LivingRoom.objects.create(user=instance)
我试图搜索这个问题,找到了一些案例,但仍然无法弄清楚如何解决它。