python+djiago博客開發3

 1.工程下的 urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),  #跳轉到django管理界面
    url(r'^blog/', include('blogapp.urls')),  #跳轉到app下的urls

]

2.app下的urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),       #blog/admin也跳轉到django管理界面
    url(r'^$', views.index),                #跳轉到首頁
    url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),#article路徑,跳轉到article頁面
]

3.views.py

from django.shortcuts import render
from . import models

# Create your views here.


def index(request):
    articles = models.Blog.objects.all()
    return render(request, "blog/index.html", {'articles': articles})


def article_page(request, article_id):
    article = models.Blog.objects.get(pk=article_id)
    return render(request, "blog/article_page.html", {'article': article})

4.templates下blog下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/{{article.pk}}'>{{article.title}}</a>
 <br/>
 {%endfor%}
</body>
</html>

 templates下blog下article_page.html

<!DOCTYPE html>
<html lang="en">  
<head>  
  <meta charset="UTF-8">  
  <title>Aticle Page</title>  
</head>  
<body>  
 <h1>{{article.title}}</h1>
 <br/>
 <h3>{{article.content}}</h3>
 <br/><br/>
 <a href=''>編輯文章</a>
</body>  
</html>

5.運行結果如下

點擊第一篇文章後的效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章