• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

一、页面实现

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">
    &<span>本文来源gaodai#ma#com搞*!代#%^码$网*</span>lt;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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用django创建一个简易的博客网站的示例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址