栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用django创建一个简易的博客网站的示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用django创建一个简易的博客网站的示例

一、页面实现

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


{% extends 'base.html' %}


  
  个人博客


欢迎来到我的博客
{% for post in posts %}
  

{{ post.title }}

{% endfor %}
{{ now }} {% block title %}欢迎来到我的博客{% endblock %} {% block headmessage %}

文章列表{% endblock %} {% block content %}
    {% for post in posts %}

  • {{ post.title }}
  • {% endfor %}
{% endblock %}





  
  {% block title %} {% endblock %}



  
    

文章分类
  • 唐诗
  • 宋词
  • 五言古诗
{% include 'header.html' %} {% block headmessage %} {% endblock %} {% block content %} {% endblock %}
{% include 'footer.html' %}





  
  post


返回上一页
{{ post.body }}

{% block footer %}
  {% if now %}
    

时间:{{ now }}

{% else %}

如需转载请注明来源

{% 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//',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 %}
{% block title %} {% endblock %}
2.方法二
{% load staticfiles %}

以上就是利用django创建一个简易的博客网站的示例的详细内容,更多关于django创建网站的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/16993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号