0
我有我想提交給我的服務器的Django形式。當提交按鈕被按下時,它發送一個post請求,並且兩個對象(一個stream_id和一個圖像)被髮送到view函數。我可以從調試頁面看到,對象分別包含POST和FILES對象中的stream_id和image。Django錯誤提交形式爲
然而,一旦視圖功能被擊中,我嘗試初始化形式如下:
uploadImageForm = UploadImageForm(request.POST, request.FILES)
並獲得我拋出驗證錯誤。
[28/May/2013 18:46:32] DEBUG [ct:194] uploadImage - method is post, errors:
<ul class="errorlist"><li>stream_id<ul class="errorlist">
<li>This field is required.</li></ul></li>
<li>image<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
[28/May/2013 18:46:32] DEBUG [ct:195] cleaned data: {}
任何想法爲什麼?
我的形式:
class UploadImageForm(forms.Form):
image = forms.ImageField()
image.widget.attrs["onchange"]="this.form.submit();"
stream_id = forms.CharField(max_length=100)
stream_id.widget.attrs["style"]="display:none;"
def __init__(self,stream_id,*args,**kwrds):
super(UploadImageForm,self).__init__(*args,**kwrds)
self.fields['stream_id'].widget.attrs["value"]= stream_id
型號:
class Stream(models.Model):
tracked_user = models.ForeignKey(TrackedUser)
stream_id = models.CharField(max_length=255)
stream_hash = models.CharField(max_length=255)
name = models.CharField(max_length=60)
start_time = models.DateTimeField(default=datetime.datetime.now(pytz.utc))
end_time = models.DateTimeField(default=None)
end_time.null = True
image = models.ImageField(upload_to="/")
image.null = True
def __unicode__(self):
return self.name
代碼在views.py:
def uploadImage(request):
uploadImageForm = None
if (request.method == "POST"):
uploadImageForm = UploadImageForm(request.POST, request.FILES)
log.debug("uploadImage - method is post, errors: " +
str(uploadImageForm.errors))
有趣的是,結合的形式輸出如下:
<p>
<label for="id_image">Image:</label>
<input id="id_image" name="image" onchange="this.form.submit();" type="file" />
</p>
<ul class="errorlist">
<li>This field is required.</li>
</ul>
<p>
<label for="id_stream_id">Stream id:</label>
<input id="id_stream_id" maxlength="100" name="stream_id"
style="display:none;" type="text" value="<QueryDict:
{u'stream_id':[u'd21256f37601d2800b0b9604f0e94e1e'],
u'csrfmiddlewaretoken':
[u'F0fmAD0VAj0RHrM0GGfnaSb6vTNgj9ZJ']}>" />
</p>
能否請您發表您的模型和形成的? – Brandon
@Brandon請看看! – khalid13