我不明白为什么如果我的 html 页面中有一个表单:<input type="submit" class="btn btn-small btn-primary" id="execute"
value="Esegui">`
那么表格是有效的,否则如果我输入一个
<input class="btn btn-small btn-primary" type="button" id="execute"
value="Esegui"> 表格是无效的。问题是,如果我将提交作为“输入类型”,则无法加载 JSON 的代码:
<script type="text/javascript">
$(document).ready(function () {
$("a # single_image").fancybox();
$("# execute").click(function () {
var execute = true
$("# execute").attr("value", "Run ...");
$("# single_image.") attr("href", "");
$("# image").attr("src", "");
$.ajax({
type: "POST",
url: "/ cloud / carica_immagine"
date: {
execute: execute
}
}).done(function (msg) {
$("# execute").attr("value", "Run");
$("# single_image.") attr("href", "graphs /" + msg["user_id"] + "/" + msg["graph"] + ."png");
$("# image").attr("src", "graphs /" + msg["user_id"] + "/" + msg["graph"] + ."png");
$("# single_image.") trigger("click");
});
});
})
</ script>.
这是我的 html 页面调用的视图:
def list(request):
# Handle file upload
documents = Document.objects.all()
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
print newdoc.docfile
newdoc.save()
...
...
return render_to_response('processa_immagine.html', {'documents':documents, 'form': form, 'user_id':request.session['user_id'],'graph':graph}, RequestContext(request))
else:
form = DocumentForm() # A empty, unbound form
# Render list page with the documents and the form
return render_to_response('processa_immagine.html', {'documents':documents, 'form': form}, RequestContext(request))
这是我的表格:
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Seleziona un file',help_text='massimo 42 megabytes',
)
这是我的模型:
class Document(models.Model):
docfile = models.FileField(upload_to='documents')
我的页面html表单是:
<form method="post" id="myform" enctype="multipart/form-data">
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }} </p>
<p>{{ form.docfile }}</p>
<input class="btn btn-small btn-primary" type="button" id="execute" value="Esegui"><br/>
</form>
我希望有人帮助我。提前致谢。