FastAPI重定向到另一个包含数据的url

2024-09-24 00:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何重定向到另一个包含数据的页面?从FastAPI文档中,我已经读到了重定向到另一个URL必须使用RedirectResponse()

@app.post('/update/{book_id}')
async def update_post(
    title = Form(...),
    author = Form(...),
    published_date = Form(...),
    isbn = Form(...),
    page_count = Form(...),
    preview_url = Form(...),
    publication_language = Form(...),
    book_id: int = 0,
):  

try:
    book: Book = await Book.objects.get(id=book_id)
except ormar.exceptions.NoMatch:
    return status.HTTP_404_NOT_FOUND

await book.update(
    title=title,
    author=author,
    published_date=published_date,
    isbn=isbn,
    page_count=page_count,
    preview_url=preview_url,
    publication_language=publication_language
)

return RedirectResponse(url='/', status_code=status.HTTP_303_SEE_OTHER , headers={'message': 'Book has been successfully updated!'})
return RedirectResponse(url='/', status_code=status.HTTP_303_SEE_OTHER , headers={'message': 'Book has been successfully updated!'})

这不起作用。我无法在我的/端点中读取此数据

 print(dict(request.__dict__['scope']['headers'])['message'])

给我 KeyError: 'message'


Tags: formidurlmessagedatetitlestatuspage