Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKyle Barney
9,081 PointsFlask BuildError When Including Link
Background: I'm building this app mostly as it appears in the lesson. The only real difference thus far is that I'm using "Entries" rather than "Posts".
I keep getting a BuildError when I include the code to create a link to the new entry page for authenticated users. This is the code that causes the error to appear (it is located in the "layout.html' file):
<a href="{{ url_for('entry') }}">Post New Entry</a>
Whenever I omit this code, the app runs as expected, which is strange because all this code should do is create a link to the "New Entry" page, right?
Thinking that there may be an issue with my route, here is that section of the main app file (fs_app.py
as I've named it):
# Create Entry
@app.route('/newentry', methods=('GET', 'POST'))
@login_required
def new_entry():
form = forms.EntryForm()
if form.validate_on_submit():
models.Entry.create(user=g.user._get_current_object(),
title=form.title.data.strip(),
content=form.content.data.strip())
flash("Entry posted.", "success")
return redirect(url_for('index'))
return render_template('entry.html', form=form)
And here is the error that is being generated:
werkzeug.routing.BuildError
BuildError: ('entry', {}, None)
Traceback (most recent call last)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 792, in decorated_view
return func(*args, **kwargs)
File "/home/kylebarney/LEARN/flasksocial/fs_app.py", line 100, in new_entry
return render_template('entry.html', form=form)
File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/kylebarney/LEARN/flasksocial/templates/entry.html", line 2, in top-level template code
{% from 'macros.html' import render_field %}
File "/home/kylebarney/LEARN/flasksocial/templates/layout.html", line 70, in top-level template code
<a href="{{ url_for('entry') }}">Post New Entry</a>
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1678, in build
raise BuildError(endpoint, values, method)
BuildError: ('entry', {}, None)
Any ideas?
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are using "{{ url_for('entry') }}" but your function name is "new_entry". Theses need to match
Kyle Barney
9,081 PointsAh, so that references the function rather than the template. Thanks!