Django部落格主頁面開發
阿新 • • 發佈:2018-12-01
一 頁面概要
-
部落格主頁面
-
部落格文章內容頁面
-
部落格撰寫頁面
二 主頁面內容
-
文章標題列表,超連結
-
發表部落格按鈕(超連結)
三 列表編寫思路
-
取出資料庫中所有文章物件
-
將物件們打包成列表,傳遞到前端
-
前端頁面把文章以標題連結的形式逐個列出
四 模板For迴圈
{% for xx in xxs %}
HTML語句
{% endfor %}
五 後端views.py開發
from django.shortcuts import render from django.http import HttpResponse from . import models def index(request): articles = models.Article.objects.all() return render(request, 'blog/index.html',{'articles': articles})
六 前端index.html開發
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> <a href="">新文章</a> </h1> {% for article in articles %} <a href="">{{ article.title }}</a> <br/> {% endfor %} </body> </html>
七 執行結果