浏览代码

DjangoGirls db-based data view

scadl 6 年之前
父节点
当前提交
cb1ed2031d
共有 2 个文件被更改,包括 9 次插入10 次删除
  1. 5 9
      blog/templates/blog/post_list.html
  2. 4 1
      blog/views.py

+ 5 - 9
blog/templates/blog/post_list.html

@@ -7,16 +7,12 @@
             <h1><a href="/">Django Girls Blog</a></h1>
         </div>
 
+        {% for post in posts %}
         <div>
-            <p>published: 14.06.2014, 12:14</p>
-            <h2><a href="">My first post</a></h2>
-            <p>Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
-        </div>
-
-        <div>
-            <p>published: 14.06.2014, 12:14</p>
-            <h2><a href="">My second post</a></h2>
-            <p>Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut f.</p>
+          <p> <i>Published:</i> {{ post.published_date }} </p>
+          <h2> <a href="">{{ post.title }}</a> </h2>
+          <p> {{ post.text|linebreaksbr }} </p>
         </div>
+        {% endfor %}
     </body>
 </html>

+ 4 - 1
blog/views.py

@@ -1,5 +1,8 @@
 from django.shortcuts import render
+from django.utils import timezone
+from .models import Post
 
 # Create your views here.
 def post_list(request):
-    return render(request, 'blog/post_list.html', {})
+    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
+    return render(request, 'blog/post_list.html', {'posts':posts})