av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

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

index.htmlbase.htmlpost.htmlheader.htmlfooter.html

<!-- index.html-->{% extends ’base.html’ %}<html lang='en'><head> <meta charset='UTF-8'> <title>個(gè)人博客</title></head><body><h1>歡迎來(lái)到我的博客</h1>{% for post in posts %} <hr> <p style='font-family: 微軟雅黑 '> <a href='http://www.4tl426be.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 %}歡迎來(lái)到我的博客{% endblock %} {% block headmessage %}<h3 style='font: 微軟雅黑;'>文章列表</h3>{% endblock %} {% block content %} <ul> {% for post in posts %}<p> <li><a href='http://www.4tl426be.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://www.4tl426be.cn/tag/?p=唐詩(shī)' rel='external nofollow' >唐詩(shī)</a></li> <li><a href='http://www.4tl426be.cn/tag/?p=宋詞' rel='external nofollow' >宋詞</a></li> <li><a href='http://www.4tl426be.cn/tag/?p=五言古詩(shī)' rel='external nofollow' >五言古詩(shī)</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' >返回上一頁(yè)</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)注明來(lái)源</p> {% endif %}{% endblock %}

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

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’標(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 makemigrationspython manage.py migrate

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

#初始頁(yè)面 顯示所有文章列表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(’/’)#返回首頁(yè)#在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, includefrom django.contrib import adminfrom django.urls import pathfrom myblogs import views#import tinymceurlpatterns = [ path(’’, views.homepage),#進(jìn)入系統(tǒng)主頁(yè) path(’admin/’, admin.site.urls),#進(jìn)入管理員頁(yè)面 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='http://www.4tl426be.cn/bcjs/{% 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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: av手机在线播放 | 久久国产欧美日韩精品 | 精品中文字幕一区二区三区 | 亚洲视频免费在线观看 | 超碰欧美 | 欧美三级在线 | 久草资源在线视频 | 九色91视频| 91精品久久 | www午夜视频 | 免费观看一级特黄欧美大片 | 亚洲成人av在线播放 | 一区二区在线 | 一区二区亚洲 | 亚洲国产黄 | 国产中文字幕av | 在线成人一区 | 日日夜夜狠狠操 | 色婷婷一区二区三区四区 | 亚洲二区视频 | 人人看人人草 | 九色一区| 亚洲精品日韩综合观看成人91 | 亚洲国产一区二区三区四区 | 国产精品毛片一区二区在线看 | 毛片.com| 欧美一级视频 | 亚洲欧美视频 | 亚洲第一天堂 | 国产中的精品av涩差av | 91精品一区 | 国产一级在线 | 欧美精品在线免费 | 久久久无码精品亚洲日韩按摩 | 无码日韩精品一区二区免费 | a级毛片国产 | 国精产品一区一区三区免费完 | 99伊人网| 天天干天天爽 | 欧美精品福利视频 | 欧美一级做性受免费大片免费 |