Mykhailo Yurchenko
Mykhailo Yurchenko

Reputation: 23

Is there the way to manualy set form variable in jinja2

Good evening to everyone! I have the following code, that doesn't work.


    <form action="{{ url_for('book') }}" method="post">
        <div>
            <input name="name" placeholder="Enter Your Name" type="text">
        </div>
        <div>
            {% set flight_id = 3 %}
        </div>
        <button>Submit</button>
    </form>

So I want to set the variable "flight_id" without any input. Is it possible to do that? I'm designing an airlines application and I have a page specially for the booking, but also, I have a page where I have information about a particular flight. Furthermore, I want to provide registration to a particular flight from that page without choosing a flight (flight with flight_id = 3 for example).
Flask code:

@app.route("/book", methods=["POST"])
def book():
    name = request.form.get("name")
    try:
        flight_id = int(request.form.get("flight_id"))
    except ValueError:
        return render_template("error.html", message="Wrong flight id")

    db.execute("INSERT INTO passengers(name, flight_id) VALUES(:name, :flight_id)", {"name":name, "flight_id":flight_id})
    db.commit()

Upvotes: 0

Views: 369

Answers (1)

StoneCountry
StoneCountry

Reputation: 159

If i right understand you, <input type="hidden"> can help you.

<input type="hidden" name="flight_id" value="3">

Place this code in your form and field flight_id will passed to server on form submit.

Upvotes: 1

Related Questions