Forums

more jinja2 issues: block "defined twice"

I get the following error:

.... File "/home/pv/phil_web/templates/user.html", line 10, in template {% block page_content %} .... jinja2.exceptions.TemplateAssertionError: block 'page_content' defined twice

However, the error refers to the following template:

{% extends "base.html" %}

{% block title %}Homepage {% endblock %}

{% block page_content %} <div class="page-header"> <h1> Hello, {{name}} </h1> </div> {% endblock %}

I don't get it: where is it defined twice?

maybe you have page_content somewhere in base.html? or in another page that uses this user.html template?

Dear Conrad,

in "base.html", I have this snippet at the end:

{% block content %} <div class="container"> {% block page_content %}{% endblock %} </div> {% endblock %}

However, I thought that because it was a distinct template, this wouldn't cause an error in user.html? I know you can't have two of the same block in a given template, and if you do, you can use the self.content() work-around, but I don't think that's applicable, here.

You're extending base.html in user.html so it inherits the contents of base.html including the page_content block.

Is this the real issue?

http://stackoverflow.com/questions/25757718/jinja2-is-duplicating-content-when-extends

No, that doesn't look anything like your issue. That user doesn't get an error and other people in the thread were unable to reproduce it and thought it might be related to how he was loading the templates or rendering the results.

I have tried running the templates that you provided through jinja2 and it works fine. My guess is that there's something somewhere else that you're missing.

The template code is OK, then? What do you suggest I try?

I didn't say that. I said the bits that you posted here are ok. Check other bits of the templates that you haven't posted here. Check that you've reloaded your web app. Check that you're loading the templates correctly. Check that the templates that you're editing are actually the templates that are being used by your web app.

I went through my error log one more time, after double-checking everything and re-loading the app, and here's what I get:

**

Traceback (most recent call last):
  File "/home/pv/.local/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/pv/.local/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/pv/.local/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/pv/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/pv/.local/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/pv/.local/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pv/phil_web/flask_app.py", line 25, in user
    return render_template('user.html', name = name)
  File "/home/pv/.local/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/home/pv/.local/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/pv/phil_web/templates/user.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/pv/phil_web/templates/base.html", line 1, in top-level template code
    {% extends "bootstrap/base.html" %}
  File "/home/pv/phil_web/templates/bootstrap/base.html", line 1, in top-level template code
    {% block doc -%}
  File "/home/pv/phil_web/templates/bootstrap/base.html", line 4, in block "doc"
    {%- block html %}
  File "/home/pv/phil_web/templates/bootstrap/base.html", line 6, in block "html"
    {%- block head %}
  File "/home/pv/phil_web/templates/bootstrap/base.html", line 13, in block "head"
    {%- block styles %}
  File "/home/pv/phil_web/templates/bootstrap/base.html", line 15, in block "styles"
    <link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
jinja2.exceptions.UndefinedError: 'bootstrap_find_resource' is undefined

**

As we've been talking about these issues on an email thread, and you gave me permission to take a look at your files over there, I took a peek. I think the problem here is that you're not telling your Flask app that it should use the flask-bootstrap plugin.

You need to put this at the top of your flask_app.py:

from flask_bootstrap import Bootstrap

...and then, where you currently have

app = Flask(__name__)

...you need to add this immediately underneath:

Bootstrap(app)

Thank you so much, Giles!

She's relentless:

2016-10-24 03:26:15,373 :Error running WSGI application Traceback (most recent call last): File "/bin/user_wsgi_wrapper.py", line 154, in call app_iterator = self.app(environ, start_response) File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application raise e File "/bin/user_wsgi_wrapper.py", line 179, in <module> application = load_wsgi_application() File "/bin/user_wsgi_wrapper.py", line 175, in load_wsgi_application return import(os.environ['WSGI_MODULE'], globals(), locals(), ['application']).application File "/var/www/www_pvernes_me_wsgi.py", line 16, in <module> from flask_app import app as application File "/home/pv/phil_web/flask_app.py", line 1, in <module> from flask import Bootstrap ImportError: cannot import name 'Bootstrap'

Here is our guide for debugging import errors: http://help.pythonanywhere.com/pages/DebuggingImportError/

I tried running python on appropriate wsgi.py file, and it still can't import Bootstrap.

Can i possibly just edit this is base.html?

<link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">

If you're getting the import error when you run the wsgi file, then you don't have the module installed. Install flask_bootstrap, which provides Bootstrap.

That template has nothing to do with the import.

I tried it again, and it said I already did?

Then your code isn't running in the same environment that you installed it in. Make sure you're installing it for the correct version of Python or, if you're using a virtualenv, into the correct virtualenv.

I installed it for both 3.5 and 2.7, and I'm running 3.5. I'm not using a ve.

here's my wsgi:

import sys

# add your project directory to the sys.path 
project_home = u'/home/pv/phil_web' if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work 
from flask_app import app as application

I just noticed in the unformatted traceback that you sent earlier: You're importing from the wrong module. The import statement that Giles suggested was:

from flask_bootstrap import Bootstrap

you have

from flask import Bootstrap

Good eye, Glenn!

I fixed it, and now my /user/xyz page works, but my index page gets a 405.

Is this a suspect:

File "/var/www/www_...._wsgi.py", line 16, in <module> from flask_app import app as application

Good eye, Glenn!

I fixed it, and now my /user/xyz page works, but my index page gets a 405.

Is this a suspect:

File "/var/www/www_...._wsgi.py", line 16, in <module> from flask_app import app as application

NOW... 405's? What the heck... and my error messages have not updated, either.

HTTP code 405 means "method not allowed", so you're trying to access a route that does not permit the method that you're using to access it. Here are the Flask docs about HTTP methods: http://flask.pocoo.org/docs/0.11/quickstart/#http-methods

I have 'GET' and 'POST' on the / route, and so I don't know what else I need to do to fix it?

FWIW, I did have a typo on my route decorate, with 'GET,POST' instead of 'GET','POST', but after fixing that, I still get a 405.

Did you reload your web app?

Yeap... still got the 405.

Then you need to go back to basics: Verify that you're actually running the code you think you're running, re-check your error log, make sure you're hitting the URL that the view is for etc.

I got this in my traceback:

Traceback (most recent call last): File "/bin/user_wsgi_wrapper.py", line 154, in call app_iterator = self.app(environ, start_response) File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application raise e File "/bin/user_wsgi_wrapper.py", line 154, in call app_iterator = self.app(environ, start_response) File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application raise e File "/bin/user_wsgi_wrapper.py", line 179, in <module> application = load_wsgi_application() File "/bin/user_wsgi_wrapper.py", line 175, in load_wsgi_application return import(os.environ['WSGI_MODULE'], globals(), locals(), ['application']).application File "/var/www/www_pvernes_me_wsgi.py", line 16, in <module> *from flask_app import app as application File "/home/pv/phil_web/flask_app.py", line 1, in <module>* from flask import Bootstrap

I noticed from your traceback that you aren't doing the import correctly. See Giles' post above.

FIXED IT!

Gents, you are all amazing: thank you so much!

Hooray! Glad you got there in the end.