Issue
I've been working on a problem set: cs50 finance. The "quote" part works as follows: (1) user fills the form of quote.html with a stock symbol (2) press submit (3) lands on quoted.html where we display some information like name of the company, stock price etc.
For an easy start I decided to use only one variable "name" and hardcode its value "Anna" into application.py to check if it appears on quoted.html.
1st case:
I hardcoded "name" in def quoted. After pressing "submit" on quote.html quoted.html shows up BUT it doesn't handle my variable "name", the place of my variable is blank, the value "Anna" doesn't appear.
2nd case:
I hardcoded "name" at the end of def quote and now the value "Anna" of my variable appears on quoted.html.
Why is that? My way of thinking is that I point to quoted.html via quote.html and when the program reach that point it should find the variable in the definition of quoted.html and use it, but obviously it is not so. So what is really happening here?
Quote.html
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/quote" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Symbol" type="text">
</div>
<button class="btn btn-primary" type="submit">Quote</button>
</form>
{% endblock %}
Quoted.html
{% extends "layout.html" %}
{% block title %}
Quoted
{% endblock %}
{% block main %}
Hello, {{ name }}
{% endblock %}
Case 1 - part of application.py
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure symbol was submitted
if not request.form.get("symbol"):
return apology("must provide symbol", 403)
# Redirect user to quoted to see data
return render_template("quoted.html")
else:
return render_template("quote.html")
@app.route("/quoted")
@login_required
def quoted():
return render_template("quoted.html", name="Anna")
Case 2 - part of application.py
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure symbol was submitted
if not request.form.get("symbol"):
return apology("must provide symbol", 403)
# Redirect user to quoted to see data
return render_template("quoted.html", name="Anna")
else:
return render_template("quote.html")
@app.route("/quoted")
@login_required
def quoted():
return render_template("quoted.html")
Solution
In case 1 and 2, if the user starts at yoursite.com/quote they will go through def quote()
and head to the else
statement because it is a GET
request. Quote.html will be rendered, this is where they fill in the fields and hit submit. Still on yoursite.com/quote so it will go through def quote()
this time as a POST
.
In case 2 it gets to
# Redirect user to quoted to see data
return render_template("quoted.html", name="Anna")
Where the name="Anna"
argument gets passed to the jinja template and rendered with {{name}}
.
In case 1 it gets to the same part of the code.
# Redirect user to quoted to see data
return render_template("quoted.html")
And now there is no hardcoded name. But I'm assuming you want it to jump to def quoted()
and render quoted.html with the name from there? Then you need:
# Redirect user to quoted to see data
return redirect(url_for("quoted"))
redirect
and url_for
can be imported from flask. More on redirects in the flask docs.
Answered By - FLSte
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.