Git Commit: blog detail view for app
We have successfully made a listview that displays a list of blogs in cards. What if someone wants to explore more about the blog? We need a detail view to display the complete details of a single blog post.
Let's add the below code in apps > v1 > route_blog.py
from db.repository.blog import retreive_blog
@router.get("/app/blog/{id}")
def blog_detail(request: Request, id: int, db: Session = Depends(get_db)):
blog = retreive_blog(id=id, db=db)
return templates.TemplateResponse(
"blog/detail.html", {"request": request, "blog": blog}
)
- I believe we have done enough routes in our previous course, so you should be already understanding this route.
Time to create the file templates > blog > detail.html
{% extends "base.html" %}
{% block title %}
<title>{{blog.title}}</title>
{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<h1 class="display-4 text-center">{{blog.title}}</h1>
<small class="text-muted">Publish Date: {{blog.created_at.strftime('%Y-%m-%d')}}</small>
<p class="text-secondary my-4">{{blog.content}}</p>
</div>
</div>
{% endblock %}
Let's try out our work at http://127.0.0.1:8000/ You should see a detail page by clicking the read more button: