python FLASK应用程序无法从HTML表单接收数据[重复]

hi3rlvi2  于 2023-01-04  发布在  Python
关注(0)|答案(2)|浏览(159)
    • 此问题在此处已有答案**:

Sending data from HTML form to a Python script in Flask(2个答案)
11小时前关门了。
我已经创建了基本的FLASK应用程序来存储学生数据,我试图从HTML表单接收数据,作为应用程序的前端。
但无法从HTML表单接收数据,请任何人推荐建议的方法或代码。

    • 我的FLASK应用程序代码**
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)
    • 我的HTML表单代码:**
<!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>
blmhpbnm

blmhpbnm1#

在前端代码中,尝试写入http://127.0.0.1:8000/students而不是http://127.0.0.1:8000/students/
末尾的“/”是URL出错的地方,也可能是您没有接收到数据的原因。

gmxoilav

gmxoilav2#

将FLASK代码中的值更改为data = request.form
更新代码以供参考。

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.form
    new_student = student(FirstName=data['FirstName'], SecondName=data['SecondName'], Section=data['Section']
    db.session.add(new_student)
    db.session.commit()
    return "Student Record Updated", 201

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(host='127.0.0.1',port=8000,debug=True)

相关问题