I recently started to learn to use the Flask framework. However, I am a bit confused on how to call function 'sign_in_check' from main.py, when a button is clicked in the html template. In Django, it is as simple as <form action = "{{% url 'sign_in_check' %}}>
. However if I try doing the same thing with Flask, it returns an error. I have been googling a solution to this with no luck. Here is my code, I am aware that flask has better built in functionality for sign in, however I still need to know how to call functions from templates because I have other pages that require a python function to run.
main.py
from flask import Flask, render_template, request, current_app
from flask_socketio import SocketIO, send
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
socketio = SocketIO(app)
@app.route('/')
def sign_in():
return render_template('sign_in.html', sign_in_check_func = sign_in_check)
@app.route('/sign_in_check')
def sign_in_check():
print("In Sign In Check")
if request.method == "POST":
driver_first_name = request.POST.get('driver_first_name', '')
driver_last_name = request.POST.get('driver_last_name', '')
driver_WWID = request.POST.get('driver_WWID', '')
co_driver_first_name = request.POST.get('co_driver_first_name', '')
co_driver_first_name = request.POST.get('co_driver_last_name', '')
co_driver_WWID = request.POST.get('co_driver_WWID', '')
car_number = request.POST.get('car_number', '')
c = {'driver_first_name':driver_first_name, 'driver_last_name':driver_last_name,
'driver_WWID':driver_WWID, 'co_driver_first_name':co_driver_first_name,
'co_driver_last_name':co_driver_last_name, 'co_driver_WWID':co_driver_WWID,}
if driver_first_name != "" and driver_last_name != "" and driver_WWID != "" and co_driver_first_name != "" and co_driver_last_name != "" and co_driver_WWID != "":
return render_template('pre_drive_inspection.html')
return render_template('sign_in.html')
sign_in.html
<!DOCTYPE html>
{% extends "base.html" %}
{% block main_content %}
<html>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/sign_in.css') }}">
<body>
<div id="driver_info">
<li>
<label for="driver_first_name">Driver First Name:</label>
<input type="text" name="driver_first_name" value="{{driver_first_name }}" id="driver_first_name">
</li>
<li>
<label for="driver_last_name">Driver Last Name:</label>
<input type="text" name="driver_last_name" value = "{{driver_last_name }}" id="driver_last_name">
</li>
<li>
<label for="driver_wwid">Driver WWID:</label>
<input type="text" name="driver_WWID" value="{{driver_WWID }}" id="driver_WWID" maxlength="8"
onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
</li>
</div>
<div id="co-driver_info" >
<li>
<label for="co_driver_first_name">CO-Driver First Name:</label>
<input type="text" name="co_driver_first_name" value="{{co_driver_first_name }}" id="co_driver_first_name">
</li>
<li>
<label for="co_driver_last_name">CO-Driver Last Name:</label>
<input type="text" name="co_driver_last_name" value="{{ co_driver_last_name }}" id="co_driver_last_name">
</li>
<li>
<label for="co_driver_wwid">CO-Driver WWID:</label>
<input type="text" name="co_driver_WWID" value="{{ co_driver_WWID }}" id="co_driver_WWID" maxlength="8"
onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
</li>
</div>
<div id="car_number">
<li>
<label for="car_number">Car Number:</label>
<input type="text" name="car_number" value="{{ car_number }}" id="co_driver_WWID" maxlength="4"
onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"> </li>
</li>
</div>
<button action="{{% url_for('sign_in_check') %}}" method="post" value = "{{ csrf_token }}">
<div id="button">
<li>
<input type="submit" value="Continue">
</li>
</div>
</button>
</body>
</html>
{% endblock %}
This is an HTML issue.
Buttons don't have actions. Buttons belong in forms, and the form has an action attribute.
<form action="{{% url_for('sign_in_check') %}}" method="post">
<input type="hidden" value = "{{ csrf_token }}">
<div id="driver_info">
<li>
<label for="driver_first_name">Driver First Name:</label>
<input type="text" name="driver_first_name" value="{{driver_first_name }}" id="driver_first_name">
</li>
...
</div>
<div id="button">
<li>
<input type="submit" value="Continue">
</li>
</div>
</form>
Note, there are lots of other things wrong with your HTML as well. For example, you can't have a body
element after a div; that div belongs in the body. And li
elements must go inside a ul
element (or ol
).
Also, your parent "base.html" template should be providing the basic page structure including the HTML declaration, and the head
and body
elements; your child template should simply fill in the space inside the body.
After more googling I figured it out. If anyone is curious, I took Daniel's advice and restructured the html. However, that didn't completely fix it. The action sytanx was:
<form action='/sign_in_check' method="POST">
and then the server handled it like so:
@app.route('/sign_in_check', methods = ['GET', 'POST'])
def sign_in_check():
if request.method == 'POST':
I had to make sure the function accepted POST or GET. Also, unlike django, the request syntax for flask is a bit different. Like so,
driver_first_name = request.form.get('driver_first_name')
Hope this helps anyone else that had similar problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.