1. 程式人生 > 程式設計 >利用django建立一個簡易的部落格網站的示例

利用django建立一個簡易的部落格網站的示例

一、頁面實現

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

<!-- index.html-->
{% extends 'base.html' %}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>個人部落格</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: 微軟雅黑">時間:{{ now }}</p>
  {% else %}
    <p style="font-family: 微軟雅黑">如需轉載請註明來源</p>
  {% endif %}
{% endblock %}

models.py 資料表的設計

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'標題')#標題
  slug = models.CharField(max_length=200,verbose_name=u'文章網址')#文章網址
  body = models.TextField()#文章內容
  tags = models.CharField(max_length=100,verbose_name=u'標籤')
  pub_date = models.DateTimeField(default = timezone.now)#發表時間

  #pub_date 以timezone.now的方式讓其自動產生時間 在執行需要pytz模組支撐
  class Meta:
    db_table = '部落格'
    ordering = ['pub_date']#按照發表時間排序顯示順序依據
    def __str__(self):#設定此類所提供的資料項,顯示文章標題
      return self.title

資料表的遷移 在cmd中執行

python manage.py makemigrations
python manage.py migrate

views.py 方法的實現

#初始頁面 顯示所有文章列表
def homepage(request):
  posts = Post.objects.all().order_by('-pub_date')
  return render(request,'index.html',locals())
  now = datetime.now()
  #顯示文章內容
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中呼叫屬於同一個標籤文章
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中對應
      return render(request,locals())
  except:
    print('沒找到')

url.py在url中註冊路徑

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),#進入系統主頁
  path('admin/',admin.site.urls),#進入管理員頁面
  path('post/<slug:slug>/',views.show_detail),#顯示詳細資訊# 定義拼接地址,獲取標籤資訊  
  url(r'^tag/$',views.search_tag)#注意這裡使用的是url 和正則表示式 需要前文中引入
  #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建立一個簡易的部落格網站的示例的詳細內容,更多關於django建立網站的資料請關注我們其它相關文章!