我如何在django管理面板上上传文本字段上的图像,就像我们在wordpress中一样

时间:2021-11-18 07:23:35

is there any way i can uplaod an image in django textfield from admin panel like we do on wordpress where we get to choose to upload image.i have used ckeditor as my textfield editor which lets me insert links and make text bolds,there is an upload image option available but it ask me for image url instead of asking me to browse an image file from my system.below is a portion of my models.py code

有没有什么方法我可以在管理面板上在django textfield中上传图片,就像我们在wordpress上做的那样我们选择上传image.i使用ckeditor作为我的文本域编辑器,它允许我插入链接并使文本变为粗体,有一个上传图片选项可用,但它问我图像网址,而不是让我从我的系统浏览图像文件。贝洛是我的models.py代码的一部分

from tinymce.models import HTMLField
from ckeditor.fields import RichTextField
from django.db import models
from datetime import datetime
from django.template.defaultfilters import slugify
from unidecode import unidecode
from django.template import defaultfilters




class Category(models.Model):
    name = models.CharField(max_length=128,unique=True)
    slug = models.SlugField(unique=True, null=True, blank=True)


    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Category, self).save(*args, **kwargs)

    def __str__(self):
        return self.name

class Heading(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=5000)
    content = RichTextField()
    image= models.ImageField(null=True,blank=True)
    date = models.DateField(default=datetime.now())
    time = models.TimeField(default=datetime.now())
    slug = models.SlugField(unique=True, null=True, blank=True)

    def save(self, *args, **kwargs):
        self.slug = defaultfilters.slugify(unidecode(self.title))
        super(Heading, self).save(*args, **kwargs)



    def __str__(self):
        return self.title

below are some images to show what i am getting right now 我如何在django管理面板上上传文本字段上的图像,就像我们在wordpress中一样

下面是一些图片,以显示我现在得到的

我如何在django管理面板上上传文本字段上的图像,就像我们在wordpress中一样

2 个解决方案

#1


2  

You can use RichTextUploadingField of django-ckeditor. you need to import it first in your models.py

您可以使用django-ckeditor的RichTextUploadingField。你需要先在models.py中导入它

from ckeditor.fields import RichTextUploadingField

now in your model you can do like this

现在你的模特你可以这样做

content = RichTextUploadingField()

By default RichTextUploadingField allows you to upload any file. you can also restrict this field to only upload images by adding this line to your settings.py

默认情况下,RichTextUploadingField允许您上传任何文件。您还可以将此字段限制为仅通过将此行添加到settings.py来上传图片

CKEDITOR_ALLOW_NONIMAGE_FILES = False

#2


0  

Uploading images like WordPress does is not a straight-forward process, although trivial to implement.

像WordPress一样上传图像并不是一个简单的过程,尽管实现起来很简单。

When you upload an image in WordPress, in the background WordPress saves the image on the server (using AJAX) and then fetches its link and displays it in the text-editor.

当您在WordPress中上传图像时,在后台WordPress将图像保存在服务器上(使用AJAX),然后获取其链接并在文本编辑器中显示它。

Because text-editors don't support file upload, you'll need to implement this yourself.

由于文本编辑器不支持文件上载,因此您需要自己实现。

But why bother when django-ckeditor app supports file upload. See docs.

但是当django-ckeditor app支持文件上传时,为什么还要烦恼呢。查看文档。

#1


2  

You can use RichTextUploadingField of django-ckeditor. you need to import it first in your models.py

您可以使用django-ckeditor的RichTextUploadingField。你需要先在models.py中导入它

from ckeditor.fields import RichTextUploadingField

now in your model you can do like this

现在你的模特你可以这样做

content = RichTextUploadingField()

By default RichTextUploadingField allows you to upload any file. you can also restrict this field to only upload images by adding this line to your settings.py

默认情况下,RichTextUploadingField允许您上传任何文件。您还可以将此字段限制为仅通过将此行添加到settings.py来上传图片

CKEDITOR_ALLOW_NONIMAGE_FILES = False

#2


0  

Uploading images like WordPress does is not a straight-forward process, although trivial to implement.

像WordPress一样上传图像并不是一个简单的过程,尽管实现起来很简单。

When you upload an image in WordPress, in the background WordPress saves the image on the server (using AJAX) and then fetches its link and displays it in the text-editor.

当您在WordPress中上传图像时,在后台WordPress将图像保存在服务器上(使用AJAX),然后获取其链接并在文本编辑器中显示它。

Because text-editors don't support file upload, you'll need to implement this yourself.

由于文本编辑器不支持文件上载,因此您需要自己实现。

But why bother when django-ckeditor app supports file upload. See docs.

但是当django-ckeditor app支持文件上传时,为什么还要烦恼呢。查看文档。