0

**difference to the suggested repeat, my error stemmed from the following line being missing in the original code session['message']=request.form['message'] wherease in the suggested duplicate was missing the render_template component`

I am trying to create user sessions with Flask, I don't care about authentication. I just want a page where they enter their name, and then they are redirected to the main page. I tried to follow the example in this link here but I get a werkzeug.routing.BuildError. To summarise my python app is:

from flask import Flask, render_template
from flask import request, session, url_for,abort,redirect

app = Flask(__name__)
app.config['SECRET_KEY'] = 'F34TF$($e34D';

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

@app.route('/signup', methods=['POST'])
def signup():
    session['username'] = request.form['username']
    session['message']=request.form['message']
    return redirect(url_for('message'))

@app.route("/message")
def message():
    return render_template("message.html")

if __name__ == '__main__':
    app.run(debug=True)

and index.html is:

{% extends "layout.html" %}
{% block content %}
    <h1>Say something</h1>
    <form method="post" action="{{ url_for('signup') }}">
        <p><label>Username:</label> <input type="text" name="username"    required></p>
        <p><button type="submit">Send</button></p>
    </form>
{% endblock %}

layout.html is:

<!doctype html>
<html lang="en">
    <head>
        <title>Say somthing</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
       <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>
laila
  • 1,009
  • 3
  • 15
  • 27
  • possible duplicate of [Flask error: werkzeug.routing.BuildError](http://stackoverflow.com/questions/3683108/flask-error-werkzeug-routing-builderror) – plaes May 29 '15 at 14:57
  • **difference to the suggested repeat, my error stemmed from the following line being missing in the original code session['message']=request.form['message'] whereas in the suggested duplicate was missing the render_template component` – laila May 29 '15 at 15:11
  • This is a simple user error that doesn't contribute much to this site. Marking it as duplicate also links it to another similar issue. – plaes May 29 '15 at 15:15
  • https://stackoverflow.com/questions/45787274/flask-werkzeug-routing-builderror/45789147#45789147 – Nabin Aug 21 '17 at 04:06

1 Answers1

1

You are getting that error because you don't have a route called message and yet you are redirecting to it.

@app.route('/signup', methods=['POST'])
def signup():
session['username'] = request.form['username']
# Create a message route first
return redirect(url_for('message'))

Here's a sample route called message

@app.route("/message")
def message():
    return render_template("message.html")
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • Thank you. I added `session['message'] = request.form['message']` to `#create a message route first` and am now getting a `The browser (or proxy) sent a request that this server could not understand.` error – laila May 28 '15 at 09:22
  • Please edit your post with your updated code. Did you create a route called message? – Bidhan May 28 '15 at 09:23
  • Why did you write session['message']=request.form['message'] inside your signup route? You don't even have a form field that takes a message. See your index.html page, there's a form field only for username, not message. – Bidhan May 28 '15 at 09:30
  • ah, bingo, I had forgotten to add that line! thank you! – laila May 28 '15 at 09:33
  • I somehow did not copy over the `

    ` part in the `index.html` file.
    – laila May 28 '15 at 09:33
  • You are welcome! Yes, it had me wondering too. Apparently the problem was with your html file and not your Python code. – Bidhan May 28 '15 at 09:36