'Django 'block' tag with name 'content' appears more than once

I got this error when making an app and I think the problem is located in my login.html as it points in the error sheet. Is it because I have 2 {% block content %} which it conflicts? Thank you for helping me

TemplateSyntaxError at /login/

'block' tag with name 'content' appears more than once

Request Method:     GET
Request URL:    http://127.0.0.1:8000/login/
Django Version:     1.4.3
Exception Type:     TemplateSyntaxError
Exception Value:    

'block' tag with name 'content' appears more than once

Error during template rendering

In template C:\djcode\mysite\drinker\templates\login.html, error at line 21
'block' tag with name 'content' appears more than once
11  <div class="register_div">
12  {% if form.password.errors %}<p class="error">{{ form.password.errors }}</p>{%     endif %}
13  <p><label for="password"{% if form.password.errors %} class="error"{% endif %}>Password:</label></p>
14  <p>{{ form.password }}</p>
15  </div>
16  <p><input type="submit" alt="register" /></p>
17  </form>
18  <p>Forgot your password? <a href="/resetpassword/">Reset it!</a></p>
19  {% endblock %}
20  {% extends "base.html" %}
21  {% block content %}

My login.html

 {% extends "base.html" %}
 {% block content %}
 <form action="" method="post">
 {% csrf_token %}
 {% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
 <div class="register_div">
         {% if form.username.errors %}<p class="error">{{ form.username.errors }}</p>{% endif %}
         <p><label for="username"{% if form.username.errors %} class="error"{% endif %}>Username:</label></p>
         <p>{{ form.username }}</p>
 </div>
 <div class="register_div">
         {% if form.password.errors %}<p class="error">{{ form.password.errors }}</p>{% endif %}
         <p><label for="password"{% if form.password.errors %} class="error"{% endif %}>Password:</label></p>
         <p>{{ form.password }}</p>
 </div>
 <p><input type="submit" alt="register" /></p>
 </form>
 <p>Forgot your password? <a href="/resetpassword/">Reset it!</a></p>
 {% endblock %}

I also got a traceback linking to my views.py

    Traceback Switch to copy-and-paste view

    C:\Python26\Lib\site-packages\django\core\handlers\base.py in get_response

                            response = callback(request, *callback_args, **callback_kwargs)

    ...
    ▶ Local vars
    C:\djcode\mysite\drinker\views.py in LoginRequest

                        return render_to_response('login.html', context,     context_instance=RequestContext(request))

    ...
    ▶ Local vars 


Solution 1:[1]

You can do the things you want to in the back-end, ie:- Python. You can for example do something like:-

# views.py::
if something is True(any condition for that matter)
msg = "something"

else:
msg = "Something else"

# template, it is here -- index.html::
{% block title %}{{msg}}{% endblock title %}

Solution 2:[2]

I think sometimes the error code maybe a little straight forward, "I think". Anyway I solved this problem by deleting the extra line tag(s), in this case the {% block content %} line tag.

Solution 3:[3]

If there is any variable that you want to render it more than once.
Just pass as input while rendering the file.
Like This:

from django.shortcuts import render

def products_list(request):
    return render(request, "products/list.html",
        {
            "title":"Products List"
        })

Inside the HTML File:

<html>

<head>
    <title>{{ title }} | My  Website</title>
</head>

<body>
    <h1>{{ title }}:</h1>
</body>

This will be rendered like this:

<html>

<head>
    <title>Products List | My  Website</title>
</head>

<body>
    <h1>Products List:</h1>
</body>

Solution 4:[4]

In my case, it was solved by changing the name of the block content.

My scenario with this error was: Some pages of my site are open to the public. Some pages of my site are free for site users to see.

Because my user registration was entered in a user authentication step, I was checking the site. If it is anonymous, it must register first, so I had two options to check. The first case (if anonymous) Ligen pages, home, rules and .. Redomination mode (if the site user) pages, profiles and apps I changed the content blocks to solve the name. In the base.html file: This base forum:

          {% block content1%}
user.is_anonymous
          {% endblock content1%}

           block% block content2%}
is_authenticated
          end% endblock content2%}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Buck
Solution 2 buddemat
Solution 3
Solution 4 amir avira