Django简易博客网站开发全流程指南
本文详细介绍利用Django框架创建简易博客网站的完整流程。内容涵盖HTML模板结构设计、models.py数据模型定义、数据库迁移命令执行、views.py视图逻辑实现以及url.py路径注册。同时包含静态资源配置及setting文件修改方法,帮助开发者快速掌握Django核心组件的协同工作方式。
首先进行页面模板的实现,构建博客前端展示的基础结构。
需要创建的HTML模板文件包括: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
完成模型定义后,需在命令行终端中执行数据迁移命令,将模型转换为数据库表结构。
python manage.py makemigrations python manage.py migrate
在views.py中编写视图函数,处理业务逻辑并返回对应的HTML响应。
#初始页面 显示所有文章列表
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.py文件,设置静态文件路径、数据库连接等关键参数。
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
在HTML模板中引入静态资源,确保CSS和JavaScript文件能正确加载。
1.方法一
{% load staticfiles %}
<title>{% block title %} {% endblock %}</title>
2.方法二
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'index.css' %}" rel="external nofollow" >
以上即为利用Django创建简易博客网站的完整示例,更多相关技术细节可参考本站其他文章。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













