DEV Community

Phansivang
Phansivang

Posted on

Django Detail View (Post Detail)

views.py

from .models import Post
from django.views.generic import DetailView

class postview(DetailView):
    model = Post (Import your post models)
Enter fullscreen mode Exit fullscreen mode

urls.py

from django.urls import path
from . import views

urlpatterns = [

    path('post/<int:pk>',views.postview.as_view(),name='post-view'),

]

Enter fullscreen mode Exit fullscreen mode

post_detail.html

{% block content %}
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
        <div class="media-body">
            <div class="article-metadata">
                <a class="mr-2" href="#">{{ post.author }}</a>
                <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
            </div>
            <h2><a class="article-title" href="{% url 'post-view' post.id %}">{{ post.title }}</a></h2>
            <p class="article-content">{{ post.body }}</p>
        </div>
    </article>
{% endblock content %}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)