利用django创建一个简易的博客网站的示例
#代码知识 发布时间: 2026-01-12
一、页面实现

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,'index.html',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创建网站的资料请关注其它相关文章!
代码知识SEO上一篇 : 基于CyclicBarrier和CountDownLatch的使用区别说明
下一篇 : SpringBoot+MongoDB实现物流订单系统的代码
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!