I want to send data from app.post()
to app.get()
using RedirectResponse
.
@app.get('/', response_class=HTMLResponse, name='homepage')
async def get_main_data(request: Request,
msg: Optional[str] = None,
result: Optional[str] = None):
if msg:
response = templates.TemplateResponse('home.html', {'request': request, 'msg': msg})
elif result:
response = templates.TemplateResponse('home.html', {'request': request, 'result': result})
else:
response = templates.TemplateResponse('home.html', {'request': request})
return response
@app.post('/', response_model=FormData, name='homepage_post')
async def post_main_data(request: Request,
file: FormData = Depends(FormData.as_form)):
if condition:
......
......
return RedirectResponse(request.url_for('homepage', **{'result': str(trans)}), status_code=status.HTTP_302_FOUND)
return RedirectResponse(request.url_for('homepage', **{'msg': str(err)}), status_code=status.HTTP_302_FOUND)
- How do I send
result
ormsg
viaRedirectResponse
,url_for()
toapp.get()
? - Is there a way to hide the data in the URL either as
path parameter
orquery parameter
? How do I achieve this?
I am getting the error starlette.routing.NoMatchFound: No route exists for name "homepage" and params "result".
when trying this way.
Update:
I tried the below:
return RedirectResponse(app.url_path_for(name='homepage')
+ '?result=' + str(trans),
status_code=status.HTTP_303_SEE_OTHER)
The above works, but it works by sending the param as query
param, i.e., the URL looks like this localhost:8000/?result=hello
. Is there any way to do the same thing but without showing it in the URL?
request.url_for()
, i.e.,get_main_data
). As for hiding the data in the URL, please take a look at this answer. – Ilonarouter
as well. Tried withget_main_data
also, same result – Nappe