最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例

 更新時(shí)間:2020年09月29日 08:52:42   作者:saiminhou  
這篇文章主要介紹了利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下

一、頁面實(shí)現(xiàn)

index.html
base.html
post.html
header.html
footer.html

<!-- index.html-->
{% extends 'base.html' %}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>個(gè)人博客</title>
</head>
<body>
<h1>歡迎來到我的博客</h1>
{% for post in posts %}
  <hr>
  <p style="font-family: 微軟雅黑 ">
  <a href="/post/{{ post.slug }}" rel="external nofollow" rel="external nofollow" >{{ post.title }}</a>
  </p>
{% endfor %}
<br>
{{ now }}
</body>
</html>
<div class="mainContext">
  <div class="rightContext">
    {% block title %}歡迎來到我的博客{% endblock %}
    {% block headmessage %}<h3 style="font: 微軟雅黑;">文章列表</h3>{% endblock %}
    {% block content %}
    <ul>
      {% for post in posts %}
        <p>
          <li><a href="/post/{{ post.slug }}" rel="external nofollow" rel="external nofollow" >{{ post.title }}</a></li>
        </p>
      {% endfor %}
    </ul>
    {% endblock %}
</div>
</div>
<!-- base.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{% block title %} {% endblock %}</title>
</head>
<body>
<div class="mainContext">
  <div class="leftContext">
    <h3 style="font: 微軟雅黑;">文章分類</h3>
    <ul>
      <li><a href="/tag/?p=唐詩" rel="external nofollow" >唐詩</a></li>
      <li><a href="/tag/?p=宋詞" rel="external nofollow" >宋詞</a></li>
      <li><a href="/tag/?p=五言古詩" rel="external nofollow" >五言古詩</a></li>
    </ul>
  </div>
  <div class="rightContext">
    <div class="top1">
    {% include 'header.html' %}
  </div>
  <div class="mid2">
    {% block headmessage %} {% endblock %}
    {% block content %} {% endblock %}
  </div>
  <div class="bot3">
    <br/>
    {% include 'footer.html' %}
  </div>
  </div>
</div>
</body>
</html>
<!-- post.html-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>post</title>
</head>
<body>
<a href="http://localhost:8000/" rel="external nofollow" >返回上一頁</a><br/>
{{ post.body }}
</body>
</html>
<!-- footer.html-->
{% block footer %}
  {% if now %}
    <p style="font-family: 微軟雅黑">時(shí)間:{{ now }}</p>
  {% else %}
    <p style="font-family: 微軟雅黑">如需轉(zhuǎn)載請(qǐng)注明來源</p>
  {% endif %}
{% endblock %}

models.py 數(shù)據(jù)表的設(shè)計(jì)

from django.db import models
from django.utils import timezone
from tinymce.models import HTMLField
# Create your models here.
class Post(models.Model):
  title = models.CharField(max_length = 200,verbose_name=u'標(biāo)題')#標(biāo)題
  slug = models.CharField(max_length=200,verbose_name=u'文章網(wǎng)址')#文章網(wǎng)址
  body = models.TextField()#文章內(nèi)容
  tags = models.CharField(max_length=100, verbose_name=u'標(biāo)簽')
  pub_date = models.DateTimeField(default = timezone.now)#發(fā)表時(shí)間

  #pub_date 以timezone.now的方式讓其自動(dòng)產(chǎn)生時(shí)間 在執(zhí)行需要pytz模塊支撐
  class Meta:
    db_table = '博客'
    ordering = ['pub_date']#按照發(fā)表時(shí)間排序顯示順序依據(jù)
    def __str__(self):#設(shè)置此類所提供的數(shù)據(jù)項(xiàng),顯示文章標(biāo)題
      return self.title

數(shù)據(jù)表的遷移 在cmd中執(zhí)行

python manage.py makemigrations
python manage.py migrate

views.py 方法的實(shí)現(xiàn)

#初始頁面 顯示所有文章列表
def homepage(request):
  posts = Post.objects.all().order_by('-pub_date')
  return render(request, 'index.html', locals())
  now = datetime.now()
  #顯示文章內(nèi)容
def show_detail(request,slug):
  try:
    post = Post.objects.get(slug = slug)
    if post != None:
      return render(request,'post.html',locals())
  except:
    return redirect('/')#返回首頁
#在views中調(diào)用屬于同一個(gè)標(biāo)簽文章
def search_tag(request): #tag在URL中獲取
  tag = request.GET.get('p')
  print(tag)
  try:
    posts = Post.objects.filter(tags=tag)#注意這里寫的是filter
    if posts != None:#這里使用的是posts,和index.html中對(duì)應(yīng)
      return render(request,'index.html',locals())
  except:
    print('沒找到')

url.py在url中注冊(cè)路徑

from django.conf.urls import url, include
from django.contrib import admin
from django.urls import path
from myblogs import views
#import tinymce
urlpatterns = [
  path('', views.homepage),#進(jìn)入系統(tǒng)主頁
  path('admin/', admin.site.urls),#進(jìn)入管理員頁面
  path('post/<slug:slug>/',views.show_detail),#顯示詳細(xì)信息# 定義拼接地址,獲取標(biāo)簽信息  
  url(r'^tag/$', views.search_tag)#注意這里使用的是url 和正則表達(dá)式 需要前文中引入
  #url(r'^tinymce/', include('tinymce.urls')), # 這是富文本編輯器
]

在界面中添加css或者是圖片

配置setting

STATIC_URL = '/static/'
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, 'static'),
]

在界面中引入

1.方法一
{% load staticfiles %}
<title>{% block title %} {% endblock %}</title>
2.方法二
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'index.css' %}" rel="external nofollow" >

以上就是利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例的詳細(xì)內(nèi)容,更多關(guān)于django創(chuàng)建網(wǎng)站的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 離線工作環(huán)境搭建的方法步驟

    Python 離線工作環(huán)境搭建的方法步驟

    這篇文章主要介紹了Python 離線工作環(huán)境搭建的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python如何輸出異常信息(行號(hào))

    Python如何輸出異常信息(行號(hào))

    這篇文章主要介紹了Python如何輸出異常信息(行號(hào))問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • pymysql的簡(jiǎn)單封裝代碼實(shí)例

    pymysql的簡(jiǎn)單封裝代碼實(shí)例

    這篇文章主要介紹了pymysql的簡(jiǎn)單封裝代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 如何真正的了解python裝飾器

    如何真正的了解python裝飾器

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于如何真正的了解python裝飾器的相關(guān)文章,需要的朋友們可以參考下。
    2020-08-08
  • python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)

    python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)

    這篇文章主要介紹了python自動(dòng)化辦公操作PPT的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • pyqt5數(shù)據(jù)庫使用詳細(xì)教程(打包解決方案)

    pyqt5數(shù)據(jù)庫使用詳細(xì)教程(打包解決方案)

    這篇文章主要介紹了pyqt5數(shù)據(jù)庫使用教程(打包解決方案),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用

    BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用

    這篇文章主要介紹了BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • centos+nginx+uwsgi部署django項(xiàng)目上線

    centos+nginx+uwsgi部署django項(xiàng)目上線

    本文主要介紹了centos+nginx+uwsgi部署django項(xiàng)目上線,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 對(duì)Python3.x版本print函數(shù)左右對(duì)齊詳解

    對(duì)Python3.x版本print函數(shù)左右對(duì)齊詳解

    今天小編就為大家分享一篇對(duì)Python3.x版本print函數(shù)左右對(duì)齊詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python 第三方日志框架loguru使用

    Python 第三方日志框架loguru使用

    使用Python自帶的logging模塊記錄日志,但是總覺得不夠優(yōu)雅。 Loguru解決了這個(gè)問題,接下來通過本文給大家介紹Python 第三方日志框架loguru使用,感興趣的朋友跟隨小編一起看看吧
    2021-05-05

最新評(píng)論

文昌市| 新邵县| 来凤县| 墨竹工卡县| 桃园县| 汶川县| 图们市| 句容市| 石嘴山市| 宁南县| 石狮市| 深泽县| 常德市| 成安县| 寿光市| 宁蒗| 黄山市| 城口县| 安达市| 丽江市| 榆社县| 武平县| 耒阳市| 库尔勒市| 丽江市| 晋宁县| 哈巴河县| 方山县| 兴安县| 冕宁县| 安西县| 繁昌县| 诏安县| 齐齐哈尔市| 梅州市| 郧西县| 安宁市| 高密市| 赣榆县| 任丘市| 博野县|