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 trial

Python Flask Basics Character Builder Forms

Redirect is not working

I was doing the exercises of the video but when the redirect function is not working for me, is there any error in my code?

from flask import Flask, render_template, redirect, url_for

app = Flask(__name__)

@app.route('/')
def aaa():
    return render_template('index.html')

@app.route('/save', methods=['POST'])
def save():
    return redirect(url_for('index'))

app.run(debug=True, host='0.0.0.0', port=8000)
{% extends "layout.html" %}
{% block content %}
<!--Enter Name -->
<div class="enter-name">
    <div class="grid-100">
        <img src="http://justcuteanimals.com/wp-content/uploads/2013/08/cute-brown-bear-cub-pictures-cutness-sweet-animal-pics.jpg" class="bear-avatar" />
    </div>
    <div class="grid-100">
        <form action="{{url_for('save')}}" method="POST">

            <label>Name your bear</label>
            <input type="text" name="name" value="" autofocus>
            <input type="submit" value="Let's build it!">
        </form>
    </div>
</div>
{% endblock %}

[MOD: added code style toblock formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

url_for('index') is looking for an app.route-decorated function named "index". Change the function named "aaa" to "index" or use url_for('aaa')