'How to redirect the user back to the home page using FastAPI, after submitting an HTML form?

I have a page with a table of students. I added a button that allows you to add a new row to the table. To do this, I redirect the user to a page with input forms.

The problem is that after submitting the completed forms, the user goes to a new empty page. How to transfer data in completed forms and redirect the user back to the table?

I just started learning web programming, so I decided to first make an implementation without using AJAX technologies.

Code:

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
    # ???


Solution 1:[1]

To start with, in cases where you return jinja2 templates, you should return a TemplateResponse, as shown in the documentation. To redirect the user to a specific page, you can use RedirectResponse. Since you do that through a POST (and not GET) method (as shown in your example), a 405 (Method Not Allowed) error would be thrown. However, thanks to @tiangolo, you can change the response status code to status_code=status.HTTP_303_SEE_OTHER and the issue will be resolved. Below is a working example. In case you need to pass path and/or query parameters to your endpoints in the future, please have a look at this or this answer as well.

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})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris