- Wagtail 是一个用 Python 编写的开源 CMS,基于 Django 框架构建。 它优雅、强大、敏捷,专注于灵活性和用户体验,为开发人员提供一个快速有吸引力的界面,可以直观地创建和组织内容。
- Wagtail 教程系列 记录了搭建博客站点的整个过程,当前站点所呈现的即是搭建过程的最新效果。
引入 wagtail-markdown
使 Wagtail
在编辑文章时支持 Markdown
语法,提高编写效率和通用性。
安装 wagtail-markdown 与相关包
pip install wagtail-markdown pip install wagtailfontawesome pip install Pygments
wagtail-markdown 安装过程会将 django和wagtail 替换为旧版本,安装后需要执行下面命令,手工修复 wagtail,django 为指定版本,其他版本未测试
pip install django==2.1 pip install wagtail==2.4
代码高亮
python 终端中,输入下面命令:
from pygments.formatters import HtmlFormatter print(HtmlFormatter().get_style_defs('.codehilite'))
输入内容拷贝存储为 pygments.css
,放入项目 /slowread/static/css
文件目录,在页面模板文件中加入一行:
<link rel="stylesheet" type="text/css" href="{% static 'path/to/pygments.css' %}">
配置内容
修改 /slowread/settings/base.py
在 INSTALLED_APPS
中增加:
INSTALLED_APPS += [ 'wagtailmarkdown', ]
修改 /slowread/blog/models.py
,增加 Markdown StreamField block
配置:
from wagtailmarkdown.blocks import MarkdownBlock class BlogPage(Page): author = models.CharField("Author", max_length=255, default="SlowRead.Net") date = models.DateField("Post date") intro = models.CharField(max_length=250) # body = RichTextField(blank=True) body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('code', CodeBlock(label='Code')), ('blockquote', blocks.BlockQuoteBlock(label='Block Quote')), ('image', ImageChooserBlock(label='Image Chooser')), ('url', blocks.URLBlock(label='URL')), ('embed', EmbedBlock(label='Embed')), ('documentchooser', DocumentChooserBlock(label='Document Chooser')), ('rawhtml', blocks.RawHTMLBlock(label='Raw HTML')), ('table', TableBlock(label='Table')), ('markdown', MarkdownBlock(label='Markdown')), ])
增加了下面一行:
('markdown', MarkdownBlock(label='Markdown')),
执行静态文件归集
python manage.py collectstatic --noinput
完成
如果一切正常,在后台管理界面编辑文章应该可以增加 Markdown
区块,并且可以即时预览/发布了。
问题备忘
VS Code 使用 virtualenv 中的 Python 环境
在项目的\.vscode\launch.json
文件修改 Python
环境路径:
"python.pythonPath": "G:\\project_demo\\venv\\Scripts\\python.exe",
Django 日志记录
参考 https://stackoverflow.com/questions/35945857/server-error-500-wagtail-admin
Try to run:
python manage.py collectstatic --noinput
Missing static files when DEBUG = False
will cause 500 server error.
To see exactly what was the issue enable logging to a file by adding the following to the settings module (generally: settings.py):
#''' LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", 'datefmt' : "%d/%b/%Y %H:%M:%S" }, 'simple': { 'format': '%(levelname)s %(message)s' }, }, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': 'your_site_name.log', 'formatter': 'verbose' }, }, 'loggers': { 'django': { 'handlers':['file'], 'propagate': True, 'level':'DEBUG', }, 'MYAPP': { 'handlers': ['file'], 'level': 'DEBUG', }, } } #'''
This will log details about the error in 'your_site_name.log' in your project directory (you can also provide an absolute path).
When finished debugging just remove the first hash '#' from the code above to comment it and keep it for future debugging.
无法加载文件...\venv\scripts\activate.ps1
错误信息:
无法加载文件...\venv\scripts\activate.ps1
,因为在此系统上禁止运行脚本。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
。
解决办法:
- 以管理员身份打开
PowerShell
- 执行命令
set-executionpolicy remotesigned
ValueError: Redis URL must specify one of the followingschemes (redis://
pip install redis-cli-cluster