Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

402
Vistas
How to redirect to dynamic URL inside FastAPI endpoint

I'm doing a feature where the user on their profile page makes changes (not related to the user model). Everything is implemented through static HTML templates. I need the user to click on the button and return to the same page (their profile page) after processing the request.

html

<td><a href="{{ url_for('decline_event_invite', pk=invite.id) }}" class="btn blue lighten-2">Accept</a></td>

endpoints.py

@router.get('/invite/{pk}/decline')
async def decline_event_invite(
        request: Request,
        pk: int,
        user_id: str = Depends(get_current_user),
        service: InviteService = Depends(),
):
    await service.invite_decline(pk)
    ...
    --> here I want redirect to user profile page 
    return RedirectResponse('DYNAMIC URL WITH ARGS')

profile.py

@router.get('/{pk}')
async def user_profile(
        request: Request,
        pk: int,
        service: UserService = Depends()
):
    user = await service.get_user_info(pk)
    events_invites = await service.get_user_events_invite_list(pk)
    return templates.TemplateResponse(
        'profile.html',
        context=
        {
            'request': request,
            'user': user,
            'events_invites': events_invites,
        }
    )

But I can't find anywhere how to do a redirect similar to the logic that applies to templates. For example:

<a href="{{ url_for('user_profile', pk=pk) }}">Sender</a>
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use url_for() function and pass the (**kwargs) path parameters.

import uvicorn
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse
import urllib
from fastapi import APIRouter

router = APIRouter()

templates = Jinja2Templates(directory="templates")

@router.get('/invite/{pk}/decline')
async def decline_event_invite(request: Request, pk: int):
    redirect_url = request.url_for('user_profile', **{ 'pk' : pk})
    return RedirectResponse(redirect_url)    

@router.get('/{pk}')
async def user_profile(request: Request, pk: int):
    return templates.TemplateResponse("profile.html", {"request": request, "pk": pk})
    
if __name__ == "__main__":
    uvicorn.run(router, host='127.0.0.1', port=8000, debug=True)

In case you need to pass query params as well, use the following sample code (make sure to import urllib):

parsed = list(urllib.parse.urlparse(redirect_url))
parsed[4] = urllib.parse.urlencode({**{ 'username' : "name"}})
new_redirect_url = urllib.parse.urlunparse(parsed)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda