python - 如何将图像添加到我的博客文章中?

标签 python django

我是 Django 新手,关注 djangogirls.com制作博客的教程。在(博客)帖子的模型中,有一个包含帖子文本的 TextField。但是,我试图做到这一点,以便我可以在该文本中以及其中的任何位置放置可变数量的图片。

我将 HTML 写入博客文本中,并使用模板标签来关闭自动转义。这使我可以在任何我想要的地方包含图像,但我想知道是否有更好的方法来实现这一点?即使有人能指出我正确的方向,我将不胜感激。

我很高兴发布代码,我只是不知道什么代码会有帮助。

最佳答案

您可以使用一些富文本编辑器,例如wyswigckeditor

这里我使用ckeditor

首先安装ckeditor

pip install django-ckeditor

那么,

项目设置.py

INSTALLED_APPS = [

    . . . .
    'ckeditor',
    'ckeditor_uploader',
    . . . . 
]

CKEDITOR_JQUERY_URL = '//code.jquery.com/jquery-3.1.1.min.js'

CKEDITOR_UPLOAD_PATH = 'uploads/'

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'full',
     },
}

项目urls.py添加

url(r'^ckeditor/', include('ckeditor_uploader.urls')),

应用程序模型.py

from ckeditor_uploader.fields import RichTextUploadingField

text = RichTextUploadingField(null=True, blank=True)

关于python - 如何将图像添加到我的博客文章中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297318/

相关文章:

python - Django 管理命令仅在将输出重定向到文件时引发 UnicodeEncodeError

python - NGINX/Gunicorn/Django-RQ : How to log each rq-worker to a different file

python - 即使基本情况是完美的,递归也不会终止

python - 修复 Pandas 中的重叠类别

python - 使用 xlsxwriter 将 pandas dataframe 写入 Excel 并包含 `write_rich_string` 格式

python - 为什么用户信息不使用django附加到mysql数据库?

python - CS50 Web 编程 - 导入 books.csv 文件时出现 Postgres SQL 错误

python - 我怎样才能在一个月的第一天对 pandas DatetimeIndex 重新采样

django - 为什么在启动 celery 时没有检测到我的环境变量?

python - 如何在 Factory Boy 中使用 lazy_attribute 和 Faker