1.3.2 查看文章内容

目录

1.编辑 blog/views.py

from django.shortcuts import render,get_object_or_404
from .models import BlogArticles

# Create your views here.
def blog_title(request):
    blogs = BlogArticles.objects.all()
    #print(blogs)
    return render(request,"blog/titles.html",{"blogs":blogs})

def blog_article(request,article_id):
    article=get_object_or_404(BlogArticles,id=article_id)
    pub=article.publish
    #return render_to_response("content.html",{"article":article,"publish":pub})
    return render(request,"blog/content.html",{"article":article,"publish":pub})

 

 

 2.编辑 templates/blog/content.html

{% extends "base.html" %}
{% block title %} block  article{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
        <h1>{{article.title}}</h1>
</div>
<div class="row">
        <div class="col-xs-12 col-md-8">
                <p class="text=-center"><span>{{article.author.username}}</span><span style="margin-left:20px">{{article.publish}}</span></p>
                <div>{{article.body}}</div>
        </div>
        <div class="col-xs-6 col-md-4">
                <h2>广告</h2>
                <p>跟老齐学 www.itdiffer.com</p>
                <!-- <img width="200px" src="http://www.mrwallpaper.com/wallpapers/Fluffy-Cat.jpg">-->
                <img width="200px" src="https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/baiduyun@2x-e0be79e69e.png">
        </div>
</div>
{% endblock %}

 

3.编辑 blog/urls.py

from django.conf.urls import url
from . import views

app_name="blog"
urlpatterns = [
    url(r'^$',views.blog_title,name="blog_title"),
    url(r'(?P<article_id>\d)/$',views.blog_article, name="blog_detail"),
]

 

4.访问浏览器  点击blog title 马克思主义

效果如下:

PS: 因为我用的是docker环境,我把容器内的 8000端口映射到宿主机的8800端口了,所以看起来 有点不一样

 

posted @ 2020-11-13 11:40  遥远小山村  阅读(130)  评论(0编辑  收藏  举报