buraq.shortcuts — API Reference
from buraq.shortcuts import render, redirect, get_object_or_404, render_to_stringrender
Section titled “render”async def render(request, template_name: str, context: dict = None) -> HTMLResponseRender a Jinja2 template and return an HTML response.
return await render(request, "posts/list.html", {"posts": posts})return await render(request, "posts/list.html") # no contextredirect
Section titled “redirect”def redirect(to: str, permanent: bool = False) -> RedirectResponseReturn an HTTP redirect response.
return redirect("/posts/")return redirect("/posts/", permanent=True) # 301 Moved Permanentlyget_object_or_404
Section titled “get_object_or_404”async def get_object_or_404(model, **kwargs) -> model_instanceFetch a single object matching kwargs, or raise HTTP 404.
post = await get_object_or_404(Post, id=pk)post = await get_object_or_404(Post, slug=slug, is_published=True)get_list_or_404
Section titled “get_list_or_404”async def get_list_or_404(model, **kwargs) -> listFetch a filtered list of objects, or raise HTTP 404 if the result is empty.
from buraq.shortcuts import get_list_or_404
posts = await get_list_or_404(Post, is_published=True)# raises 404 if no published posts existUse get_object_or_404 when you need a single object; use get_list_or_404 when you need at least one result from a filtered set.
render_to_string
Section titled “render_to_string”def render_to_string( template_name: str | list[str], context: dict = None, request=None,) -> strRender a template to a string without returning an HTTP response. Accepts a single template name or a list (tries each in order).
from buraq.shortcuts import render_to_string
html = render_to_string("emails/welcome.html", {"user": user})
# Try multiple templates — first one that exists winshtml = render_to_string(["widgets/custom.html", "widgets/default.html"], {"items": items})See Template Loader for the full API including get_template() and select_template().