Rustango docs

Build a slug detail view with get_object_or_404

Problem

A detail handler should return a clean 404 when the slug doesn't exist.

use rustango::shortcuts::get_object_or_404;

async fn article_detail(
    State(pool): State<Pool>,
    Path(slug): Path<String>,
) -> Result<Html<String>, RustangoError> {
    let article = get_object_or_404(
        Article::objects().filter(Article::slug.eq(slug)),
        &pool,
    ).await?;
    Ok(render("article.html", context! { article }))
}

List variant

get_list_or_404 404s when a filtered list is empty — handy for tag/category pages.