I couldn't quite find what I was looking for online, so I'm hoping maybe someone could better explain this.
I have a button that does a POST method to pick a model of a car. After you choose the model it creates several different buttons below for different trims. This all works great, but I need to determine the "model" that was chosen first. So I'd like to be able to send set a variable, and send it on the render_template. Essentially, what I need is to be able to
def order_page(model=None):
ORDERCONFIGURATORLABEL = "Please Select one of the available Models below"
CAR_DATA = [
{'name': 'Model 3', 'trim': ['Standard Range', 'Long Range']},
{'name': 'Model Y', 'trim': ['Standard Range', 'Long Range']},
{'name': 'Model S', 'trim': ['Standard Range', 'Long Range']},
{'name': 'Model X', 'trim':['Standard Range', 'Long Range']}
]
if request.method == "POST":
if request.form.get("Model 3 Button"):
CAR_DATA = [
{'name': 'Model 3', 'trim': 'Standard Range', 'price': '$35,000', 'picture':'model3.png'},
{'name': 'Model 3', 'trim': 'Standard Range Plus', 'price': '$39,990', 'picture':'model3.png'},
{'name': 'Model 3', 'trim': 'Mid Range', 'price': '$45,000', 'picture':'model3.png'},
{'name': 'Model 3', 'trim': 'Long Range', 'price': '$49,990', 'picture':'model3.png'},
{'name': 'Model 3', 'trim': 'Performance', 'price': '$56,990', 'picture':'model3.png'}]
return render_template("order.html", model="model3", CAR_DATA=CAR_DATA,ORDERCONFIGURATORLABEL = "", currentmodel = "Model 3",SELECTEDMODELLABEL="Great choice! Please select your trim.", currentcarimage=r"model3.png")
I could obviously type in /order/model3 but I need to be able to set this link within python. Is there anyway I could use render_template to set /order/ or am I completely using this incorrectly?
So simply, I first check if the button sent the value "Model 3 Button", but when I click on the second button for trim, I lose that value. Is there anyway to save or "preserve" that data into my next POST method?
Here's my layout.html
<!doctype html>
<html>
<head>
<title>{{title}}</title>
<link rel="stylesheet" href="{{ url_for('static',
filename='css/main.css') }}">
<meta charset="utf-8">
<meta name="description" content={{description}}>
<link rel="shortcut icon" href="/favicon.ico">
</head>
<body >
{% include 'navigation.html' %}
{% block content %}{% endblock %}
</body>
</html>
and my order.html
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<h1 text-align: center> {{title}}</h1>
<div class="content">
<p>{{paragraph1}}</p>
<p>{{paragraph2}}</p>
</div>
</div>
<div class="column c80">
<img src="{{url_for('static', filename=currentcarimage) }}" class="carimage" alt="" />
<footer>$49000</footer>
</div>
<div class="column c20">
<h1 > Model Selector </h1>
<label class="clabel" >{{ORDERCONFIGURATORLABEL}}</label>
<div class="mybuttonparent">
</div>
<div class="mybuttonparent">
<form method="POST" action="/order">
<button class="btn" type="submit" name="Model 3 Button" value="Model 3 Button">Model 3</button>
</form>
</div>
<div class="mybuttonparent">
<form method="POST" action="/order">
<button class="btn" type="submit" name="Model Y Button" value="Model Y Button">Model Y</button>
</form>
</div>
<div class="mybuttonparent">
<form method="POST" action="/order">
<button class="btn" type="submit" name="Model S Button" value="Model S Button">Model S</button>
</form>
</div>
<div class="mybuttonparent">
<form method="POST" action="/order">
<button class="btn" type="submit" name="Model X Button" value="Model X Button">Model X</button>
</form>
</div>
<div><h2>{{currentmodel}}</h2></div>
<div><label class="clabel">{{SELECTEDMODELLABEL}}</label></div>
{% for trim in CAR_DATA%}
<div class="mybuttonparent">
<form method="POST" action="/order">
<button class="btn" type="submit" name='{{trim.trim}}' value='{{trim.trim}}'>{{trim.trim}}</button>
</form>
</div>
{% endfor %}
{% endblock %}
I tried using the
<input name="userselectedcar" value={{selectedmodel}} id="userselectedcar" />
and tried getting the value to check, but it's null.
carmodeltype = request.form.get("userselectedcar")
I passed selectedmodel into my render_template, and I can see it in the input. THen when I click on the second button, I seem to lose it because carmodeltype == None, not my model i chose ("model3")
CodePudding user response:
What you need to do is to render hidden
field in every form you want to get back the data on the "server" side.
<input type="hidden" id="car_model" name="car_model" value="{model_goes_here}}">
CodePudding user response:
I ended up figuring out a solution to my problem.
I ended up using a Flask session to hold the data from the last POST method
to save the data:
session['car_model'] = 'model3'
and to retreive the data:
carmodel = session.get('car_model', None)