From ef364f42cf56c1c4651d9746c63c24b05d51d44c Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sun, 5 Jun 2016 23:20:06 +0200 Subject: [PATCH] working on news detail view --- news/models.py | 1 + news/templates/news_detail.html | 9 +++++++++ news/templates/news_index.html | 2 +- news/urls.py | 1 + news/views.py | 8 +++++++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 news/templates/news_detail.html diff --git a/news/models.py b/news/models.py index 6afba3a9..2e4b7d9e 100644 --- a/news/models.py +++ b/news/models.py @@ -14,6 +14,7 @@ class NewsItem(CreatedUpdatedModel): content = models.TextField() public = models.BooleanField(default=False) published_at = models.DateTimeField() + slug = models.SlugField() def __str__(self): return self.title diff --git a/news/templates/news_detail.html b/news/templates/news_detail.html new file mode 100644 index 00000000..7ccde145 --- /dev/null +++ b/news/templates/news_detail.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} +{% load commonmark %} + +{% block content %} +
+

{{ news_item.title }} {{ news_item.published_at|date:"Y-m-d" }}

+
+ {{ news_item.content|commonmark }} +{% endblock %} diff --git a/news/templates/news_index.html b/news/templates/news_index.html index c44faae1..809b4adc 100644 --- a/news/templates/news_index.html +++ b/news/templates/news_index.html @@ -4,7 +4,7 @@ {% block content %} {% for item in news_items %}
-

{{ item.title }} {{ item.published_at|date:"Y-m-d" }}

+

{{ item.title }} {{ item.published_at|date:"Y-m-d" }}

{{ item.content|commonmark }} {% if not forloop.last %} diff --git a/news/urls.py b/news/urls.py index 9f1c4708..6bd938d3 100644 --- a/news/urls.py +++ b/news/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ url(r'^$', views.NewsIndex.as_view(), name='index'), + url(r'(?P[-_\w+]+)/$', NewsDetail.as_view(), name='detail'), ] diff --git a/news/views.py b/news/views.py index 1afd6c21..2cf4b9e0 100644 --- a/news/views.py +++ b/news/views.py @@ -1,4 +1,4 @@ -from django.views.generic import ListView +from django.views.generic import ListView, DetailView from . import models @@ -7,3 +7,9 @@ class NewsIndex(ListView): queryset = models.NewsItem.objects.public() template_name = 'news_index.html' context_object_name = 'news_items' + + +class NewsDetail(DetailView): + model = models.NewsItem + template_name = 'news_detail.html' +