Python Forum
(beginner): Hello Shopping - wat?(Flask) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: (beginner): Hello Shopping - wat?(Flask) (/thread-5773.html)



(beginner): Hello Shopping - wat?(Flask) - Peter_EU - Oct-20-2017

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


RE: (beginner): Hello Shopping - wat?(Flask) - buran - Oct-20-2017

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>")


RE: (beginner): Hello Shopping - wat?(Flask) - Peter_EU - Oct-20-2017

(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!!!! ^_^