我想在django表单提交后在推特-Bootstrap模式下显示last_item,但是我不知道如何处理该模式。我尝试了documentation中建议的表单按钮,但它不处理表单数据。我该怎么做?
<button data-toggle="modal" data-target="#myModal2">Submit</button>views.py
def main(request):
if request.method == 'POST':
form = MyModelForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
request.session['name'] = name
mm = MyModel.objects.create(name=name)
mm.save()
return HttpResponseRedirect('/') # Redirect after POST
else:
form = MyModelForm()
args = {}
args['last_item'] = MyModel.objects.all().order_by('pk').reverse()[0]
args['form'] = form
return render(request, 'form.html', args)form.html
{% extends "base.html" %}
{% block content %}
<form method="POST" id="" action="">
{% csrf_token %}
{{ form.as_p }}
<button>Submit</button>
</form>
<div class="modal" id="myModal2" tabindex="-1" role="dialog"
aria-labelledby="myModal2Label" aria-hidden="true" style="display: none">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModal2Label">Modal header</h3>
</div>
<div class="modal-body">
<p>Last item: {{ last_item }}</p>
</div>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}发布于 2012-09-29 18:03:06
它像bootstrap一样在单击时调用event.preventDefault(),这样可以防止表单被提交。
你应该在这个按钮和close the modal programaticaly上点击bind your own event。
它可能看起来像这样:
$('form').submit(function() {
$('#myModal2').modal('hide');
})我没有测试这段代码,但这应该是一个很好的开始。
https://stackoverflow.com/questions/12649539
复制相似问题