Twitter-Bootstrap模式中的简单Django表单
我正在尝试使用Twitter-Bootstrap模式运行django表单。 我想知道如何返回/
提交表单后我该怎么做。 我的views
和templates
如下。
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 %} Modal header
Last item: {{ last_item }}
{% endblock %} {% block scripts %} {% endblock %}
评论转换为答案。
HttpResponseRedirect('/')
而不是'/add'
?