本文介绍了简单的Django表单在Twitter-Bootstrap模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用Twitter-Bootstrap模式运行django表单。我想知道在提交表单后,我应该怎么做才能返回到 /
我的视图
和模板
在下面。 url。 py
urlpatterns = patterns('myapp.views',
url(r'^ $','main' ),
url(r'^ add /','form_add'),
)
views.py
def main(request):
if request.method =='POST' :
form = MyModelForm(request.POST)
如果form.is_valid():
name = form.cleaned_data ['name']
request.session ['name'] = name
mm = MyModel.objects.create(name = name)
mm.save()
返回HttpResponseRedirect('/ add')#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)
def form_add(request):
args = {}
name = request.session ['name']
return render(request,'add.html',args)
form.html
%扩展base.html%}
{%block content%}
< button type =buttondata-toggle =modal
data-target = #myModal1>启动模式< / button>
< div class =modalid =myModal1tabindex = - 1role =dialog
aria-labelledby =myModal1Labelaria-hidden =true style =display:none>
< div class =modal-header>
< button type =buttonclass =closedata-dismiss =modalaria-hidden =true>×< / button>
< h3 id =myModal1Label>模态标题< / h3>
< / div>
< div class =modal-body>
< form method =POSTid =action =>
{%csrf_token%}
{{form.as_p}}
< button>提交< / button>
< / form>
< / div>
< / div>
< p>最后一项:{{last_item}}< / p>
{%endblock%}
{%block scripts%}
{%endblock%}
解决方案
评论转换为回答。
HttpResponseRedirect /')
而不是'/ add'
?
I'm trying to run django forms with Twitter-Bootstrap modals. I'd like to know what should I do to return to /
after form submission. My views
and templates
are below.
url.py
urlpatterns = patterns('myapp.views',
url(r'^$', 'main'),
url(r'^add/', 'form_add'),
)
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('/add') # 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)
def form_add(request):
args = {}
name = request.session['name']
return render(request, 'add.html', args)
form.html
{% extends "base.html" %}
{% block content %}
<button type="button" data-toggle="modal"
data-target="#myModal1">Launch modal</button>
<div class="modal" id="myModal1" tabindex="-1" role="dialog"
aria-labelledby="myModal1Label" aria-hidden="true" style="display: none">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModal1Label">Modal header</h3>
</div>
<div class="modal-body">
<form method="POST" id="" action="">
{% csrf_token %}
{{ form.as_p }}
<button>Submit</button>
</form>
</div>
</div>
<p>Last item: {{ last_item }}</p>
{% endblock %}
{% block scripts %}
{% endblock %}
解决方案
Comment converted to answer.
HttpResponseRedirect('/')
instead of '/add'
?
这篇关于简单的Django表单在Twitter-Bootstrap模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!