Python Forum

Full Version: (beginner): Hello Shopping - wat?(Flask)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hellowings,

sorry for lame question for lame problem, maybe someone will find joy in helping me:

I created .py file with following code:

from flask import Flask, render_template
app = Flask(__name__)


@app.route("/")
@app.route("/<user>")

def index(user = None):
    return render_template("user.html", user=user)

@app.route("/shopping")
def shopping():
    food = ["Cheese", "Tuna", "Beef"]
    return render_template("shopping.html", food = food)





if __name__ == "__main__":
    app.run(debug=True)
        
created directory template, created HTML doc named shopping.html with:

<!doctype html>

<ul>

    {% for item in food %}
    <h1> "this should work </h1>
        <li>{{ item }}</li>
    {% endfor %}

</ul>
and when I hit Run and go at 127.0.0.1:5000/shopping instead of juicy beef I see text "Hello Shopping".


What is wrong, please?

Sincerely,

Noob Noob

WEIT!


I am just reading file named user.html in the same directory and with code

<!doctype html>

{% if user %}
    <h1>Hello {{ user }} </h1>
{% else %}
    <h1> Please log in</h1>

{% endif %}
butt why is it calling user.html instead of shopping.html?

....or is it not?... O.o

confused...

...lookin...lookin... and nuthin... 0.0
Looking at your code, it parse "/shopping" as the route @app.route("/<user>"), because it fit the route pattern
I would make it @app.route("/users/<user>")
(Oct-20-2017, 07:04 PM)buran Wrote: [ -> ]Looking at your code, it parse "/shopping" as the route @app.route("/<user>"), because it fit the route pattern
I would make it @app.route("/users/<user>")


HOLY COW IT WERKS!!1!

Thanx! This forum is awsum!!!! ^_^