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.运行结果如下

点击第一篇文章后的效果

发布了36 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/veray/article/details/86726602
今日推荐