Python Forum
(beginner): Hello Shopping - wat?(Flask)
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(beginner): Hello Shopping - wat?(Flask)
#1
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
Reply
#2
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>")
Reply
#3
(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!!!! ^_^
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020