1. 安装访问(https://docs.wagtail.io/en/v2.8/getting_started/index.html)
# 创建 python3 虚拟环境 virtualenv -p python3 venvpy36new --no-site-packages # 下载安装wagtail pip install wagtail # 简单使用 wagtail start mysite $ pip install -r requirements.txt $ ./manage.py migrate $ ./manage.py createsuperuser $ ./manage.py runserver
2. 中文和时区汉化
/settings/base.py 文件内 语言/时区设置如下: LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
3. 模型构建
from django.db import models from django import forms from modelcluster.fields import ParentalKey from wagtail.core.models import Page, Orderable from wagtail.core.fields import RichTextField from wagtail.admin.edit_handlers import FieldPanel, InlinePanel from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.search import index class BlogIndexPage(Page): intro = RichTextField(blank=True) content_panels = Page.content_panels + [ FieldPanel('intro', classname="full") ] class Meta: verbose_name = "新闻栏目" verbose_name_plural = "新闻栏目" def get_context(self, request): # Update context to include only published posts, ordered by reverse-chron context = super().get_context(request) blogpages = self.get_children().live().order_by('-first_published_at') # make the variable 'resources' available on the template context['blogpages'] = blogpages return context class BlogPage(Page): date = models.DateField("发布日期") intro = models.CharField("文章来源", max_length=250) body = RichTextField("文章内容", blank=True) upfile = models.FileField("视频附件", blank=True, upload_to="videos") search_fields = Page.search_fields + [ index.SearchField('intro'), index.SearchField('body'), ] class Meta: verbose_name = "新闻内容" verbose_name_plural = "新闻内容" content_panels = Page.content_panels + [ FieldPanel('date'), FieldPanel('intro'), InlinePanel('gallery_images', label="图像(可选)"), FieldPanel('upfile'), # label="视频"), FieldPanel('body', classname="full"), ] def main_image(self): gallery_item = self.gallery_images.first() if gallery_item: return gallery_item.image else: return None class BlogPageGalleryImage(Orderable): name = "图片长廊" page = ParentalKey(BlogPage, on_delete=models.CASCADE, related_name='gallery_images') image = models.ForeignKey( 'wagtailimages.Image', on_delete=models.CASCADE, related_name='+' ) caption = models.CharField('图片说明' ,blank=True, max_length=250) panels = [ ImageChooserPanel('image'), FieldPanel('caption'), ]
4.1 首页模板
{% extends "base.html" %} {% load wagtailcore_tags wagtailimages_tags %} {% block body_class %}template-homepage{% endblock %} {% block content %} <div class="container"> <div class="text-center"> <img src= "/static/images/yqmap.png" width="80%" /> 替换为疫情动态图地址,上面的图片删除 <iframe name="myiframe" id="myrame-record-query" src="https://yiqing.ahusmart.com/" frameborder="0" align="middle" width="80%" height="700px" scrolling="yes"> </iframe> </div> <hr> <div class="card-deck mb-2"> {% for post in yiqing %} {% with post=post.specific %} <div class="card" style=" 18em;"> {% with post.main_image as main_image %} {% if main_image %} {% image main_image fill-320x240 as header_image %} <a href="{% pageurl post %}"> <img src="{{ header_image.url }}" class="card-img-top" /> </a> {% endif %} {% endwith %} <div class="card-body"> <h3 class="card-title"><a href="{% pageurl post %}">{{ post.title }}</a></h5> <p class="card-text"> {% if post.intro %} {{ post.intro|richtext }} {% else %} {{ post.body|richtext|truncatewords_html:80 }} {% endif %} </p> </div> <div class="card-footer"> <small class="text-muted"><a href="{% pageurl post %}" class="btn btn-primary">阅读全文</a></small> </div> </div> {% endwith %} {% endfor %} </div> <div class="row"> <div class="col-md-6"> <h3 class="title_bg">上级指示</h3> <ul class="list-group"> {% for post in notice %} <a href="{% pageurl post %}" class="list-group-item">{{ post.title|richtext|truncatewords_html:25 }}</a> {% endfor %} </ul> </div> <div class="col-md-6"> <a href="/上级规定"><h3 class="title_bg">上级规定</h3></a> <ul class="list-group"> {% for post in guiding %} <a href="{% pageurl post %}" class="list-group-item">{{ post.title|richtext|truncatewords_html:25 }}</a> {% endfor %} </ul> </div> </div> <div class="row"> <div class="col-md-6"> <a href="/疫情动态"><h3 class="title_bg2">疫情动态</h3></a> <ul class="list-group"> {% for post in budui %} <a href="{% pageurl post %}" class="list-group-item">{{ post.title|richtext|truncatewords_html:25 }}</a> {% endfor %} </ul> </div> <div class="col-md-6"> <a href="/防控措施"><h3 class="title_bg2">防控措施</h3></a> <ul class="list-group"> {% for post in cuoshi %} <a href="{% pageurl post %}" class="list-group-item">{{ post.title|richtext|truncatewords_html:25 }}</a> {% endfor %} </ul> </div> </div> <div class="container"> <img src="/static/images/logoyqfk.png" width="100%" /> <a href="常识教育"> <h3 id="fkcs" class="title_bg">常识教育</h3></a> <div class="row"> {% for post in changshi %} {% with post=post.specific %} <div class="col-md-4"> {% with post.main_image as main_image %} {% if main_image %} {% image main_image fill-320x240 as header_image %} <a href="{% pageurl post %}"> <img class="img-thumbnail" src="{{ header_image.url }}" width="100%"/> <p class="text-info text-center">{{ post.title }}</p> </a> {% endif %} {% endwith %} </div> {% endwith %} {% endfor %} </div> </div> <hr> <a class="nav-link text-center" href="http://18.xx.xx.xxx/">更多文章</a> <hr> {% endblock %}
4.2. 新闻栏目页
<!-- blog/templates/blog/blog_index_page.html --> {% extends "base.html" %} {% load wagtailcore_tags wagtailimages_tags %} {% block body_class %}template-blogindexpage{% endblock %} {% block content %} <div class="box-container newscontent"> {% for post in blogpages %} {% with post=post.specific %} <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2> <p class="text-muted">{{ post.date}} | {{ post.intro }} </p> {% with post.main_image as main_image %} {% if main_image %} {% image main_image fill-320x200 %} {% elif post.upfile %} <video class="col-md-4"> <source src="/media/{{ post.upfile }}" type="video/mp4"> <source src="/media/{{ post.upfile }}" type="video/ogg"> <source src="/media/{{ post.upfile }}" type="video/webm"> 浏览器不支持,请下载谷歌浏览器播放 </video> {% endif %} {% endwith %} <div class="newscategory">{{ post.body|richtext|truncatewords_html:2 }}</div> {% endwith %} <hr /> {% endfor %} </div> {% endblock %}
4.3. 新闻内容页
blog/templates/blog/blog_page.html
{% extends "base.html" %} {% load wagtailcore_tags wagtailimages_tags %} {% block body_class %}template-blogpage{% endblock %} {% block content %} <div class="box-container"> <img src="/static/images/疫情.jpg" width="100%" height="250px" /> <h1 class="page-header text-center"> <div class="newstitle">{{ page.title }}</div> </h1> <div class="row text-center"> <br /> <p class="meta date col-md-6">发布时间: {{ page.date }}</p> <div class="intro col-md-6">信息来源: {{ page.intro }}</div> </div> <hr /> <div class="newscontent xx-large"> {{ page.body|richtext }} {% if page.upfile %} <video width='100%' controls> <source src='/media/{{ page.upfile }}' type='video/mp4'> <source src='/media/{{ page.upfile }}' type='video/ogg'> <source src='/media/{{ page.upfile }}' type='video/webm'> 抱歉,浏览器不支持该视频,请下载安装谷歌浏览器 </video> {% endif %} {% for item in page.gallery_images.all %} <div class="container text-center"> {% image item.image width-900 %} <p>{{ item.caption }}</p> </div> {% endfor %} </div> <p class="text-muted text-center"><a href="{{ page.get_parent.url }}" style="color:#222">【返回上级】</a> <a href="javascript:self.print();" class="btn-a" style="color:#222;">【打印本页】</a> </p> </div> {% endblock %}