国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

利用django創建一個簡易的博客網站的示例

瀏覽:133日期:2024-09-21 15:03:19

一、頁面實現

index.htmlbase.htmlpost.htmlheader.htmlfooter.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='http://m.cgvv.com.cn/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='http://m.cgvv.com.cn/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='http://m.cgvv.com.cn/tag/?p=唐詩' rel='external nofollow' >唐詩</a></li> <li><a href='http://m.cgvv.com.cn/tag/?p=宋詞' rel='external nofollow' >宋詞</a></li> <li><a href='http://m.cgvv.com.cn/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 modelsfrom django.utils import timezonefrom 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 makemigrationspython 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,’index.html’,locals()) except: print(’沒找到’)

url.py在url中注冊路徑

from django.conf.urls import url, includefrom django.contrib import adminfrom django.urls import pathfrom myblogs import views#import tinymceurlpatterns = [ 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='http://m.cgvv.com.cn/bcjs/{% static ’index.css’ %}' rel='external nofollow' >

以上就是利用django創建一個簡易的博客網站的示例的詳細內容,更多關于django創建網站的資料請關注好吧啦網其它相關文章!

標簽: Django
相關文章:
主站蜘蛛池模板: 黄色欧美网站 | 高级毛片| 成人黄页 | 永久免费不卡在线观看黄网站 | 国产超清在线观看 | 久久久久久综合成人精品 | 理论片我不卡在线观看 | 欧美人与鲁交大毛片免费 | 在线国产欧美 | 亚洲伊人色一综合网 | 亚洲日本在线观看视频 | 国内精品久久久久影院免费 | 亚洲欧美精品一区天堂久久 | 岛国精品成人 | 亚洲一片| 久久aaa| 最新亚洲精品国自产在线 | 亚洲视频三级 | 成人做爰全过程免费看网站 | 欧美三级不卡在线观看视频 | 国产素人在线观看 | 台湾三级香港三级在线理论 | 在线看国产 | 99久久免费观看 | 欧美成人免费一区在线播放 | 国产原创视频在线 | 欧美一级淫片免费播放口 | 韩国巨胸女三级视频网 | 天干天干天啪啪夜爽爽色 | 亚洲欧美日韩中文字幕在线一区 | 亚洲欧美日韩综合在线一区二区三区 | 午夜影院h| 亚洲精品在线看 | 毛片免费在线 | 97在线视频观看 | 亚洲欧美久久精品一区 | 欧美毛片网站 | 国产精品极品 | 在线观看 国产 | 国产在线一区观看 | 国产夫妇肉麻对白 |