AnswerDotAI / fasthtml

The fastest way to create an HTML app
https://fastht.ml/
Apache License 2.0
5.05k stars 204 forks source link

[BUG] FastHTML by Example should use a redirect for POST instead of render #389

Open zdrummond opened 2 weeks ago

zdrummond commented 2 weeks ago

Describe the bug This is more a "bug"/question about the tutorial. I was following along and noticed when I got to the WebPage->Web App section that things did not work in the browser.

  1. There seemed to be some loop (that I can not repo) that caused the page to keep adding the last item I submitted
  2. After you add an items and get back to the home page, if you refresh the page it wants to do another form submission

Feels like both issues are because the POST renders the home page instead of redirecting

@app.post("/")
def add_message(data:str):
    messages.append(data)
    return home()

vs

@app.post("/")
def add_message(data: str):
    messages.append(data)
    return RedirectResponse("/", status_code=302)

Expected behavior If I am follow a demo, the demo produces best practice web patterns. I had to go looking through GitHub example repos to even find that RedirectResponse is the right way to do this.

Environment Information Please provide the following version information:

Confirmation Please confirm the following:

Additional context Note: problem #1 might have been because I was using an earlier version of fasthtml and recently updated, but #2 to exists still.