问题描述:

I don't understand why if I have in my html page in a form: <input type="submit" class="btn btn-small btn-primary" id="execute" value="Esegui">`

then the form is valid otherwise if I put a

<input class="btn btn-small btn-primary" type="button" id="execute" value="Esegui">

the form is invalid.

The problem is that if I put as "input type" the submit then fails to load JSON's code:

<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>.

This is my view that is called by html page:

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))

This is my forms:

class DocumentForm(forms.Form):

docfile = forms.FileField(

label='Seleziona un file',help_text='massimo 42 megabytes',

)

This is my models:

class Document(models.Model):

docfile = models.FileField(upload_to='documents')

my page html with form is:

<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>

I hope that someone help me.

Thanks in advance.

网友答案:

This code is very confused. You have defined an Ajax function on your button triggered on the click event. So, when you click the button, that function is triggered, and it tries to submit the form. However, it cannot do so in a way that will make the form valid: the only field in the form is a required FileField, which cannot be submitted via a standard Ajax POST - and even if it could, your Ajax function makes no attempt to actually include it.

The reason why the input version appears to work is that the Javascript does not prevent the default submit action, so the process is this: the Ajax is run, but even before it has a chance to receive a response, the entire page is submitted, and refreshes with the result of the (valid) form submission.

相关阅读:
Top