(1)settings.pycss
MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace("//","/") MEDIA_URL = '/media/'
(2)website/urlshtml
添加圖片的url前端
from django.conf.urls import url,include from django.contrib import admin from django.conf import settings from django.conf.urls.static import static urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/',include('blog.urls') ), ] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT ) #添加圖片的url
(3)blog/models.pypython
添加兩個方法git
class Entry(models.Model): . . . def get_absolute_url(self): #獲取當前博客詳情頁的url return reverse("blog:blog_detail",kwargs={"blog_id":self.id}) #app名字,詳情頁url的別名,參數是當前博客的id def increase_visiting(self): #訪問量加1 self.visiting += 1 self.save(update_fields=['visiting']) #只保存某個字段
(4)views.pygithub
from django.shortcuts import render from . import models def index(request): entries = models.Entry.objects.all() return render(request,'blog/index.html',locals()) def detail(request,blog_id): entry = models.Entry.objects.get(id=blog_id) entry.increase_visiting() return render(request,'blog/detail.html',locals())
(5)index.pyweb
{% extends 'blog/base.html' %} {% block title %}博客首頁{% endblock %} {% block content %} <div class="container"> <div class="row"> <div class="col-md-9"> {% for entry in entries %} <h2><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a></h2> <br> {% if entry.img %} <img src="{{ entry.img.url }}" width="60%" height="270px"/> {% endif %} {% if entry.abstract %} <p>{{ entry.abstract }}</p> {% else %} <p>{{ entry.body|truncatechars:180 }}</p> {% endif %} <p> <span>做者:{{ entry.author }}</span> <span> 發佈時間:{{ entry.created_time }}</span> <span> 閱讀數:{{ entry.visiting }}</span> </p> {% endfor %} </div> </div> </div> {% endblock %}
(6)detail.htmldjango
{% extends 'blog/base.html' %}
{% block title %}博客詳情頁{% endblock %}
{% block content %}
博客{{ blog_id }}的詳情頁
{% endblock %}
detail.html編程
{% extends 'blog/base.html' %} {% block title %}博客詳情頁{% endblock %} {% block content %} <div class="container"> <div class="row"> <div class="col-md-9"> <h1>{{ entry.title }}</h1> <p> <strong>{{ entry.author }}</strong> {{ entry.created_time|date:'Y年m月d日' }} 分類: {% for category in entry.category.all %} <a href="#">{{ category.name }}</a> {% endfor %} 標籤: {% for tag in entry.tags.all %} <a href="#">{{ tag.name }}</a> {% endfor %} 瀏覽量: {{ entry.visiting }} {% if entry.img %} <img src="{{ entry.img.url }}" width="60%" height="270px"/> {% endif %} <hr /> <p> {{ entry.body }} </p> </p> </div> </div> </div> {% endblock %}
(1)安裝模塊markdown
pip install markdown
pip install pygments
(2)views.py
import markdown,pygments def detail(request,blog_id): entry = models.Entry.objects.get(id=blog_id) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) entry.body = md.convert(entry.body) entry.toc = md.toc entry.increase_visiting() return render(request,'blog/detail.html',locals())
(3)detail.html
把github.css放到blog/css裏面,detail.html引用樣式
{% extends 'blog/base.html' %} {% load staticfiles %} {% block title %}博客詳情頁{% endblock %} {% block css %} <link rel="stylesheet" href="{% static 'blog/css/github.css' %}"> {% endblock %}
標籤和正文都加salf
(4)後臺添加博客
Markdown語法測試篇
## 1.python語言介紹 編程語言主要從如下幾個角度進行分類:編譯型,靜態型,動態性,強類型定義語言和弱類型定義語言 - 編譯型:有一個負責翻譯的程序來對咱們的源代碼進行轉換,生成對應的可執行代碼,這個過程就是編譯(Compile),而負責編譯的程序就被稱爲編譯器(Compiler) - 一般咱們所說的動態語言,靜態語言是指動態類型語言和靜態類型語言 ## 2.python的優缺點 - 優勢:簡單、開發效率高、高級語言、可移植性、可擴展性、可嵌入性 - 缺點:速度慢,可是相對的、代碼不能加密、線程不能利用多CPU問題 ## 3.高階函數 ```python def func(): print('in the func') return foo() def foo(): print('in the foo()') return 666 res = func() print(res) ```
前端顯示效果: