Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, the the problem, I think, is you're expecting "magic".</p> <p>Django's views are templates are pretty dumb. They only have the variables available to them that are passed in one of two ways:</p> <ol> <li>Through a context_processor (see Aidan's answer)</li> <li>Passed in when calling render (or render_to_response)</li> </ol> <p>The reason the form renders when you visit <code>/login</code>, but in no other case, is because the Django view you're using <code>django.contrib.auth.views.login</code>, passed the Django AuthenticationForm into your template for you.</p> <p>Review the source here: <a href="https://github.com/django/django/blob/master/django/contrib/auth/views.py" rel="nofollow">https://github.com/django/django/blob/master/django/contrib/auth/views.py</a></p> <p>No other view "magically" gets that variable set.</p> <p>So you have to either:</p> <ol> <li>Set it yourself in every view across your whole app</li> <li>Use a context processor.</li> </ol> <p>Here's an easy <code>context_processors.py</code> which uses the Django stuff (based on Aidan's):</p> <pre><code>def include_login_form(request): from django.contrib.auth.forms import AuthenticationForm form = AuthenticationForm() return {'login_form': form} </code></pre> <p>Then, do as Aidan points out:</p> <pre><code>TEMPLATE_CONTEXT_PROCESSORS = ( #.... 'yourapp.context_processors.include_login_form' ) </code></pre> <p>Now, the problem is that, when you just include the template, the login form is in a variable called "login_form", but when django's built-in view uses your template, it's in a variable called "form".</p> <p>So that's an ordeal without a good "clean" solution, though I'm sure one could sort it out.</p> <p>Maybe check the context passed in in your context_processor, and if a variable already exists called "form", see if it's an instance of AuthenticationForm, and if it is, use it instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload