我在装上一种表格,使用舱载()功能,迄今为止,它正在做出色的工作。 我要补充的是,如果提交表格时出现错误,就会把错误标记重新装入人口编组,而不是改用目前实际的纸页。
有些人建议我使用jquery-form。 我认为,这将完全奏效。 我不知道如何执行。
此处为有效载荷功能:
$(document).ready(function(){
$(".create").on("click", function(){
$("#popupContact").load("/cookbook/createrecipe #createform");
});
});
The page thatloads the form:
<div id="popupContact" class="popup">
<a id="popupContactClose" style="cursor:pointer;float:right;">x</a>
<p id="contactArea">
</p>
</div>
<div id="backgroundPopup">
</div>
<div id="col2-footer">
{% paginate %}
</div>
我的形式模板如下:
<div id="createform">
<h1>Create New Recipe</h1>
<form id="createrecipe" action="{% url createrecipe %}" method="POST">
<table>
{% csrf_token %}
{{ form.as_table }}
</table>
<p><input type="submit" value="Submit"></p>
</form>
</div>
here is my attempt to use the jquery-form:
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
var options ={
target: .popup ,
};
$( #createrecipe ).submit(function() {
$(this).ajaxSubmit(options);
return false;
});
});
</script>
形成反感:
def createrecipe(request):
if not request.user.is_authenticated():
return HttpResponseRedirect( /index/ )
else:
if request.method == POST :
print 1
form = RecipeForm(request.POST)
if form.is_valid():
print 2
recipe = form.save(commit=False)
recipe.original_cookbook = request.user.cookbooks.all()[0]
recipe.pub_date = datetime.datetime.now()
recipe.save()
user = request.user
cookbooks = user.cookbooks
cookbook = cookbooks.all()[0]
cookbook.recipes.add(recipe)
return HttpResponseRedirect( /account )
else:
form = RecipeForm()
return render_to_response( cookbook/createrecipe.html ,
{ form :form},
context_instance=RequestContext(request))
thank you snackerfish