Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Invalid block tag 'form.username'

Tags:

python

django

I'm new to Django and I'm trying to create a login page.

Here's (part of) my urls.py file:

urlpatterns = patterns('',
    (r'^$', main_page),
    (r'^login/$', 'django.contrib.auth.views.login'),
)

And here's the template for my login page (registration/login.html):

<html>
<head>
<title>User Login</title>
</head>

<body>
    <h1>User Login</h1>
    {% if form.errors %}
    <p>username and password don't match.</p>
    {% endif %}
    <form method="post" action=".">
        <p>
            <label for="id_username">Username:</label> 
                    {% form.username %}
        </p>
        <p>
            <label for="id_password">Password:</label> 
                    {% form.password %}
        </p>
        <input type="hidden" name="next" value="/" /> 
            <input type="submit" name="login" />
    </form>
</body>
</html>

When I start the application and go to login.html I receive the following error message:

TemplateSyntaxError at /login/

Invalid block tag: 'form.username'

I don't understand what went wrong. As far as I know, the login view is supposed to load this template and pass the form object. When printed, the form.username attribute is supposed to generate HTML code for the username text field. Why doesn't that happen?

like image 512
snakile Avatar asked Feb 24 '23 18:02

snakile


1 Answers

form is a context variable, not a template tag. So access it via {{ form }} or {{ form.username }}!

like image 147
Bernhard Vallant Avatar answered Feb 27 '23 09:02

Bernhard Vallant