title: Django login form in bootstrap popup
body: <p>Using django 1.5 + twitter bootstrap.</p>
  
  <p>Have a popup dropdown (bootstrap) login form on every page.
  Authentificating with right credentials working good.
  Now, my problem - need to add error message on wrong credentials on the same page, without page reloading. It can be a modal window, or better if it would be just a text in the same popup window. Dunno how to do that, without redirect/reload.</p>
  
  <p>Some code:</p>
  
  <h2>views.py:</h2>
  
  <pre><code>def LoginRequest(request):
      if request.user.is_authenticated():
          return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
      if request.method == 'POST':
          form = LoginForm(request.POST)
          if form.is_valid():
              email = form.cleaned_data['email']
              password = form.cleaned_data['password']
              user = authenticate(email=email, password=password)
              if user is not None:
                  login(request, user)
                  return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
              else:
                  return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
          else:
              return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
      else:
          form = LoginForm()
          return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
  </code></pre>
  
  <h2>urls.py:</h2>
  
  <pre><code>urlpatterns = patterns(
      'accounts.views',
      url(r'^signup/$', 'AccountRegistration'),
      url(r'^login/$', 'LoginRequest'),
      url(r'^logout/$', 'LogoutRequest'),
  )
  </code></pre>
  
  <h2>forms.py:</h2>
  
  <pre><code>class LoginForm(forms.Form):
          email = forms.CharField(label=(u'Email'), max_length=30)
          password = forms.CharField(label=(u'Pass'), widget=forms.PasswordInput(render_value=False), max_length=30)
  </code></pre>
  
  <h2>Fragment of template with my popup:</h2>
  
  <pre><code>&lt;ul class="nav pull-right"&gt;
      {% if user.is_authenticated %}
      &lt;li&gt;&lt;a href="/profile/"&gt;{{ user }}&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;
          &lt;form style="margin:0;" action='/logout/' method="POST"&gt;
          {% csrf_token %}
              &lt;input type="submit" value="Logout" class="btn btn-link" style="color:#777777;" /&gt;&lt;/li&gt;
              &lt;input type="hidden" name="next" value="{{ current_path }}" /&gt;
          &lt;/form&gt;
      &lt;/li&gt;
      {% else %}
      &lt;li class="dropdown"&gt;
          &lt;a class="dropdown-toggle" href="#" data-toggle="dropdown"&gt;Login &lt;strong class="caret"&gt;&lt;/strong&gt;&lt;/a&gt;
          &lt;div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;"&gt;
              &lt;form action='/login/' method="POST"&gt;
              {% csrf_token %}
                  {% if form.errors %}
                      {{ form.errors}}
                  {% endif %}
                  &lt;label for="id_email"&gt;Login:&lt;/label&gt;
                  &lt;input type="text" name="email" id="id_email"&gt;
                  &lt;br /&gt;
                  &lt;label for="id_password"&gt;Pass:&lt;/label&gt;
                  &lt;input type="password" name="password" id="id_password"&gt;
                  &lt;br /&gt;
                  &lt;input class="btn btn-primary" type="submit" value="Login" /&gt;
                  &lt;input type="hidden" name="next" value="{{ current_path }}" /&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>