I have created the basic FLASK app to store student data and I am trying to receive the data from HTML form that is acting as frontend for the app.
But not able to receive the data from HTML form, can anyone please recommend the suggested approach or code.
MY FLASK APP CODE
import os.path
import random
import string
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'students.db')
db = SQLAlchemy(app)
class student(db.Model): #Table Declaration & Schema
id = db.Column(db.Integer, primary_key=True)
FirstName = db.Column(db.String(100), nullable=False)
SecondName = db.Column(db.String(20), nullable=False)
Section = db.Column(db.Text, nullable=False)
@app.route('/students', methods=['POST'])
def create_student():
data = request.get_json()
new_student = student(FirstName=data['FirstName'], SecondName=data['SecondName'], Section=data['Section']
db.session.add(new_student)
db.session.commit()
return request.form
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(host='127.0.0.1',port=8000,debug=True)
My HTML FORM Code:
<!DOCTYPE html>
<html>
<head>
<title>Create Student Entry</title>
</head>
<form action="http://127.0.0.1:8000/students/" method="post">
<label for="FirstName">FirstName:</label><br>
<input type="text" id="FirstName" name="FirstName"><br>
<label for="SecondName">SecondName:</label><br>
<input type="text" id="SecondName" name="SecondName"><br>
<label for="Section">Section:</label><br>
<input type="text" id="Section" name="Section"><br>
<input type="submit" value="Submit">
</form>
</html>