sto utilizzando Django per creare un utente e un oggetto quando l'utente viene creato. Ma c'è un errore__init __() ha ottenuto un argomento di parole chiave imprevisto 'utente'
__init__() got an unexpected keyword argument 'user'
quando si chiama la funzione register()
in view.py. La funzione è:
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))
e la classe oggetto è:
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)
La pagina di errore Django comunica le informazioni di errore: user=User.objects.create_user(username, email, password)
e LivingRoom.objects.create(user=instance)
Ho provato a cercare questo problema , trovando alcuni casi, ma ancora non riesco a capire come risolverlo.
La mia ipotesi è che non funziona qui: 'create_control_livingroom' è possibile disattivare il segnale e provare? e puoi mostrare anche lo stacktrace? – karthikr
Perché si limita il metodo 'LivingRoom .__ init__' a * just *' temp' allora? –