title: Simple Django form in Twitter-Bootstrap modal
body: <p>I'm trying to run django forms with Twitter-Bootstrap modals. I'd like to know what should I do to return to <code>/</code> after form submission. My <code>views</code> and <code>templates</code> are below.</p>
  
  <h3>url.py</h3>
  
  <pre><code>urlpatterns = patterns('myapp.views',
      url(r'^$', 'main'),
      url(r'^add/', 'form_add'),
  )
  </code></pre>
  
  <h3>views.py</h3>
  
  <pre><code>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)
  </code></pre>
  
  <h3>form.html</h3>
  
  <pre><code>{% extends "base.html" %}
  
  {% block content %}
  &lt;button type="button" data-toggle="modal"
          data-target="#myModal1"&gt;Launch modal&lt;/button&gt;
  
  &lt;div class="modal" id="myModal1" tabindex="-1" role="dialog"
       aria-labelledby="myModal1Label" aria-hidden="true" style="display: none"&gt;
    &lt;div class="modal-header"&gt;
      &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt;
      &lt;h3 id="myModal1Label"&gt;Modal header&lt;/h3&gt;
    &lt;/div&gt;
    &lt;div class="modal-body"&gt;
      &lt;form method="POST" id="" action=""&gt;
        {% csrf_token %}
        {{ form.as_p }}
        &lt;button&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;p&gt;Last item: {{ last_item }}&lt;/p&gt;
  {% endblock %}
  
  {% block scripts %}
  {% endblock %}
  </code></pre>