0

我想从一些数据集开始。当我这样做时,给我一个错误(init()得到了一个意外的关键字参数'user')。为什么?

if request.method == 'POST': # If the form has been submitted...
        form = UcitelZmenaForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            form = UcitelZmenaForm(request.POST)
            form.save()
            return HttpResponseRedirect('/hlavni_stranka/') # Redirect after POST
    else:
        form = UcitelZmenaForm(user=request.user) # An unbound form

class Clovek(models.Model):
    user=models.OneToOneField(User)
    mesto=models.CharField(max_length=100, blank=True)



class Ucitel(Clovek): #pridat pote classu tridniho ucitele
    predmety=models.ManyToManyField(Predmety) #manyToManyField

class UcitelZmenaForm(ModelForm):
    class Meta:
        model = Ucitel
4

1 回答 1

2

你应该改变

form = UcitelZmenaForm(user=request.user)

form = UcitelZmenaForm(initial={'user': request.user})

另请参阅: Django Forms 文档中的“动态初始值”

于 2013-10-02T13:33:46.970 回答