-
-
Save s3itz/3f7c3f0ed9b47e6e8312 to your computer and use it in GitHub Desktop.
Error with macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% from "macros.html" import nav_link with context %} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Flask Template Example</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<style> | |
div#main { | |
max-width: 500px; | |
padding: 20px; | |
} | |
nav .title { | |
font-weight: bold; | |
} | |
a.active { | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body> | |
<ul class="nav navbar-nav"> | |
{{ nav_link('home', 'Home') }} | |
{{ nav_link('about', 'About') }} | |
{{ nav_link('contact', 'Contact Us') }} | |
</ul> | |
<div id="main"> | |
<h2>This is part of my base template</h2> | |
<br> | |
{% block content %}{% endblock %} | |
<br> | |
<h2>This is part of my base template</h2> | |
</div> | |
</body> | |
<footer> | |
{% block footer %} | |
Watch! This part of the footer is coming from the base template.... | |
<br> | |
<br> | |
{% endblock %} | |
</footer> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro nav_link(endpoint, name) %} | |
{% if request.endpoint.endswith(endpoint) %} | |
<a href="{{ url_for(endpoint) }}" class="active">{{name}}</a> | |
{% else %} | |
<a href="{{ url_for(endpoint) }}">{{name}}</a> | |
{% endif %} | |
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from datetime import datetime | |
app = Flask(__name__) | |
@app.route('/') | |
def template_test(): | |
return render_template('template.html', my_string='Wheeeee!', my_list=[0,1,2,3,4,5]) | |
@app.route("/home") | |
def home(): | |
return render_template('template.html', | |
my_string="I'm the home page", | |
my_list=[0,1,2,3,4,5], | |
) | |
''' | |
@app.route("/about") | |
def about(): | |
return render_template('template.html', | |
my_string="I'm the about page", | |
my_list=[0,1,2,3,4,5], | |
) | |
@app.route("/contact") | |
def contact(): | |
return render_template('template.html', | |
my_string="I'm the contact page", | |
my_list=[0,1,2,3,4,5], | |
current_time=datetime.datetime.now() | |
) | |
@app.template_filter("fomatdate") | |
def datetimefilter(value, format='%Y/%m/%d %H:%M'): | |
"""Convert a datetime to a different format.""" | |
return value.strftime(format) | |
''' | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Flask Template Example</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<style> | |
div#main { | |
max-width: 500px; | |
padding: 20px; | |
} | |
nav .title { | |
font-weight: bold; | |
} | |
a.active { | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="main"> | |
{% extends "base.html" %} | |
{% block content %} | |
<h3> This is the start of my child template</h3> | |
<br> | |
<p>My string: {{my_string}}</p> | |
<p>Value from the list: {{my_list[1]}}</p> | |
<p>Loop through the list:</p> | |
<ul> | |
{% for n in my_list %} | |
<li>{{n}}</li> | |
{% endfor %} | |
</ul> | |
<h3> This is the end of my child template</h3> | |
{% endblock %} | |
</div> | |
</body> | |
{% block footer %} | |
{{super()}} | |
...and this addition to the footer is coming from the child template. | |
{% endblock %} | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
werkzeug.routing.BuildError | |
BuildError: ('about', {}, None) | |
Traceback (most recent call last) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ | |
return self.wsgi_app(environ, start_response) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app | |
response = self.make_response(self.handle_exception(e)) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception | |
reraise(exc_type, exc_value, tb) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app | |
response = self.full_dispatch_request() | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request | |
rv = self.handle_user_exception(e) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception | |
reraise(exc_type, exc_value, tb) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request | |
rv = self.dispatch_request() | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request | |
return self.view_functions[rule.endpoint](**req.view_args) | |
File "/home/action/thinkful/projects/jinja_example/run.py", line 8, in template_test | |
return render_template('template.html', my_string='Wheeeee!', my_list=[0,1,2,3,4,5]) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template | |
context, ctx.app) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/templating.py", line 110, in _render | |
rv = template.render(context) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render | |
return self.environment.handle_exception(exc_info, True) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception | |
reraise(exc_type, exc_value, tb) | |
File "/home/action/thinkful/projects/jinja_example/templates/template.html", line 23, in top-level template code | |
{% extends "base.html" %} | |
File "/home/action/thinkful/projects/jinja_example/templates/base.html", line 25, in top-level template code | |
{{ nav_link('about', 'About') }} | |
File "/home/action/thinkful/projects/jinja_example/templates/macros.html", line 5, in template | |
<a href="{{ url_for(endpoint) }}">{{name}}</a> | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/helpers.py", line 312, in url_for | |
return appctx.app.handle_url_build_error(error, endpoint, values) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error | |
reraise(exc_type, exc_value, tb) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for | |
force_external=external) | |
File "/home/action/thinkful/projects/jinja_example/env/lib/python2.7/site-packages/werkzeug/routing.py", line 1649, in build | |
raise BuildError(endpoint, values, method) | |
BuildError: ('about', {}, None) | |
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. | |
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. | |
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: | |
dump() shows all variables in the frame | |
dump(obj) dumps all that's known about the object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment