• <small id='TYclV'></small><noframes id='TYclV'>

    <legend id='TYclV'><style id='TYclV'><dir id='TYclV'><q id='TYclV'></q></dir></style></legend>
    • <bdo id='TYclV'></bdo><ul id='TYclV'></ul>

        <i id='TYclV'><tr id='TYclV'><dt id='TYclV'><q id='TYclV'><span id='TYclV'><b id='TYclV'><form id='TYclV'><ins id='TYclV'></ins><ul id='TYclV'></ul><sub id='TYclV'></sub></form><legend id='TYclV'></legend><bdo id='TYclV'><pre id='TYclV'><center id='TYclV'></center></pre></bdo></b><th id='TYclV'></th></span></q></dt></tr></i><div id='TYclV'><tfoot id='TYclV'></tfoot><dl id='TYclV'><fieldset id='TYclV'></fieldset></dl></div>
        <tfoot id='TYclV'></tfoot>

        返回FastAPI主页

        Return to the home page FastAPI(返回FastAPI主页)
      1. <legend id='aHBzU'><style id='aHBzU'><dir id='aHBzU'><q id='aHBzU'></q></dir></style></legend>
          <bdo id='aHBzU'></bdo><ul id='aHBzU'></ul>
        • <tfoot id='aHBzU'></tfoot>

            • <small id='aHBzU'></small><noframes id='aHBzU'>

              <i id='aHBzU'><tr id='aHBzU'><dt id='aHBzU'><q id='aHBzU'><span id='aHBzU'><b id='aHBzU'><form id='aHBzU'><ins id='aHBzU'></ins><ul id='aHBzU'></ul><sub id='aHBzU'></sub></form><legend id='aHBzU'></legend><bdo id='aHBzU'><pre id='aHBzU'><center id='aHBzU'></center></pre></bdo></b><th id='aHBzU'></th></span></q></dt></tr></i><div id='aHBzU'><tfoot id='aHBzU'></tfoot><dl id='aHBzU'><fieldset id='aHBzU'></fieldset></dl></div>

                    <tbody id='aHBzU'></tbody>
                  本文介绍了返回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', 'r', encoding='utf-8') as file:
                          html = file.read()
                  
                      # Loading a page
                      return Response(html, media_type='text/html')
                  
                  
                  # Processing forms and adding a new entry
                  @app.post('/add')
                  def add(name: str = Form(...), surname: str = Form(...), _class: str = Form(...)):
                      add_student(name, surname, _class)  # Adding student data
                      # ???
                  

                  推荐答案

                  首先,在返回jja2模板的情况下,应该返回TemplateResponse,如documentation所示。要将用户重定向到特定页面,可以使用RedirectResponse。由于您是通过POST(而不是GET)方法(如您的示例所示)执行此操作,因此将抛出405(不允许使用的方法)错误。不过,感谢@tiangolo,您可以change the response status code转到status_code=status.HTTP_303_SEE_OTHER,问题就解决了。下面是一个工作示例。如果将来需要将路径和/或查询参数传递给终结点,请同时查看this或this answer。

                  from fastapi import FastAPI, Request, Form, status
                  from fastapi.templating import Jinja2Templates
                  from fastapi.responses import RedirectResponse
                  
                  app = FastAPI()
                  templates = Jinja2Templates(directory="templates")
                  
                  # replace with your own get_students() method
                  def get_students():
                      return ["a", "b", "c"]
                  
                  @app.post('/add')
                  async def add(request: Request, name: str = Form(...), surname: str = Form(...), _class: str = Form(...)):
                      # add_student(name, surname, _class)  # Adding student data
                      redirect_url = request.url_for('index')    
                      return RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER)    
                  
                  @app.get('/add_student')
                  async def add_student_page(request: Request):
                      return templates.TemplateResponse("add_student.html", {"request": request})
                  
                  @app.get('/')
                  async def index(request: Request):
                      students = get_students()  # Get a list of students
                      return templates.TemplateResponse("index.html", {"request": request, "students": students})
                  

                  这篇关于返回FastAPI主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                  <tfoot id='eYtSh'></tfoot>

                          • <bdo id='eYtSh'></bdo><ul id='eYtSh'></ul>
                              <tbody id='eYtSh'></tbody>
                            <legend id='eYtSh'><style id='eYtSh'><dir id='eYtSh'><q id='eYtSh'></q></dir></style></legend>

                            <small id='eYtSh'></small><noframes id='eYtSh'>

                            <i id='eYtSh'><tr id='eYtSh'><dt id='eYtSh'><q id='eYtSh'><span id='eYtSh'><b id='eYtSh'><form id='eYtSh'><ins id='eYtSh'></ins><ul id='eYtSh'></ul><sub id='eYtSh'></sub></form><legend id='eYtSh'></legend><bdo id='eYtSh'><pre id='eYtSh'><center id='eYtSh'></center></pre></bdo></b><th id='eYtSh'></th></span></q></dt></tr></i><div id='eYtSh'><tfoot id='eYtSh'></tfoot><dl id='eYtSh'><fieldset id='eYtSh'></fieldset></dl></div>