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