Skip to content

feat: add missing key arguments to TemplateResponse #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ch37n1
Copy link

@ch37n1 ch37n1 commented Feb 4, 2024

This change adds support for status_code headers media_type background params

This change adds support for status_code headers media_type background params
@maces
Copy link
Owner

maces commented Mar 2, 2024

Hi there thanks very much for pointing this out. Sry for the late reply.
I still want to check if it maybe makes sense to have this typed better + later maybe have an additional response model which then also support other HTMX features (like oob updates), before changing the public API.

@maces maces added the enhancement New feature or request label Mar 2, 2024
@hf-kklein
Copy link

Do I understand it correctly, that this would allow to send response headers? I'm especially interested on how to send an HX-Trigger header in my response

@ch37n1
Copy link
Author

ch37n1 commented Apr 3, 2024

Yes. This feature adds full compatibility with Jinja2 Templates with full backward compatibility. Some htmx functionality uses headers, so i added support to use it in my project. Also this request adds ability to change status_code and media_type.

Example usage with headers:

@router.get("/update_cart/{product_id}/{action}", response_class=HTMLResponse)
@htmx('parts/cart_item', 'parts/cart_item')
def update_cart(
        request: fastapi.Request,
        response: fastapi.Response,
        product_id: int,
        action: str,
        cart_service = fastapi.Depends(dependencies.get_cart_service)):
    session_id = request.session[auth.consts.USER_ID_SESSION_KEY]
    if action == 'increment':
        item, _ = cart_service.add_to_cart(product_id, session_id)
    if action == 'decrement':
        item, _ = cart_service.remove_from_cart(product_id, session_id)
    return {
            'item': item,
            'headers': {'HX-Trigger': 'my-custom-event'},
            }

@maces
Copy link
Owner

maces commented Apr 4, 2024

Short update: I'm still working on some refactoring I want to merge beforehand. But after that I'm looking forward to this as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants