小编Bil*_*bab的帖子

提交 HTML 表单后,如何使用 FastAPI 将用户重定向回主页?

我有一个包含学生表格的页面。我添加了一个按钮,允许您向表中添加新行。为此,我将用户重定向到带有输入表单的页面。

问题是,提交完成的表单后,用户会转到一个新的空白页面。如何传输已完成表单中的数据并将用户重定向回表格?

我刚刚开始学习Web编程,所以我决定先不使用AJAX技术来实现。

代码:

from fastapi import FastAPI, Form
from fastapi.responses import Response

import json
from jinja2 import Template

app = FastAPI()


# The page with the table
@app.get('/')  
def index():
    students = get_students()  # Get a list of students
    with open('templates/students.html', 'r', encoding='utf-8') as file:
        html = file.read()
    template = Template(html)  # Creating a template with a table

    # Loading a template
    return Response(template.render(students=students), media_type='text/html')


# Page with forms for adding a new entry
@app.get('/add_student')
def add_student_page():
    with open('templates/add_student.html', …
Run Code Online (Sandbox Code Playgroud)

python http jinja2 web fastapi

4
推荐指数
1
解决办法
6048
查看次数

标签 统计

fastapi ×1

http ×1

jinja2 ×1

python ×1

web ×1