我是一個真正的web開發初學者。以下代碼在is_valid()
檢查中失敗。但我不明白爲什麼:表單應該從POST數據中獲取其數據?Django form is_valid()失敗
型號:
class Statement(models.Model):
text = models.CharField(max_length=255)
user = models.ForeignKey(User)
time = models.DateField()
views = models.IntegerField()
的ModelForm:
class StatementForm(ModelForm):
class Meta:
model = Statement
widgets = {
'time':forms.HiddenInput(),
'user':forms.HiddenInput(),
'views':forms.HiddenInput(),
}
View功能:
def new(request):
if request.method == 'POST': # If the form has been submitted...
form = StatementForm(request.POST) # A form bound to the POST data
if form.is_valid():
stmt = form.save()
path = 'stmt/' + stmt.id
return render_to_response(path, {'stmt': stmt})
else:
c = {}
c.update(csrf(request))
loggedin_user = request.user
d = datetime.now()
form = StatementForm(request.POST, initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})
return render_to_response('new_stmt.html', {'form': form, },context_instance=RequestContext(request))
我發現了類似的主題,並嘗試了很多。這是我認爲它應該工作。我真的需要建議。
轉到你是否在接受最佳答案(如果這些是一個)特定問題前面的問題。您可以通過在答案旁放置複選標記來完成此操作。 –