简体   繁体   English

编码 base64 中的 ImageField 并将其存储在 DB 中而不是图像的路径中

[英]Encode ImageField in base64 and store it in DB instead of path of the image

settings.py设置.py

STATIC_URL = '/static/'
STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))]
STATIC_ROOT = BASE_DIR.joinpath('staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = str(BASE_DIR.joinpath('media'))

config/urls.py配置/urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('proj.urls')),
] += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py模型.py

class Book(models.Model):
    image = models.ImageField(upload_to='upload/', blank=True, default=None, null=True)

views.py视图.py

class BookCreateView(CreateView):
    model = Book
    template_name = "book_new.html"
    fields = "__all__"

book_new.html book_new.html

{% extends 'base.html' %}

{% block content %}
    <h1>New Book</h1>
    <form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Save">
    </form>
{% endblock content %}

proj/urls.py项目/urls.py

urlpatterns = [
    path("", home_view, name="home"),
    path("book/new/", BookCreateView.as_view(), name="book_new"),
]

I have written this code which will upload the image to /media/upload/ and the path of the file will be saved on the database like upload/image_file.png .我已经编写了这段代码,它将图像上传到/media/upload/并且文件的路径将保存在数据库中,如upload/image_file.png I don't want to store the file path in the database, I want to read the file, convert it to base64 and store the encoded string in the database.我不想将文件路径存储在数据库中,我想读取文件,将其转换为 base64 并将编码的字符串存储在数据库中。 How can I do that?我怎样才能做到这一点? I've searched a lot and tried to figure this out on my own as I usually do, but this one's a little hard for me, here's what I've read so far:我已经搜索了很多,并试图像往常一样自己解决这个问题,但这对我来说有点难,这是我到目前为止所读到的:

ImageField , as far as the database is concerned, is a simple VARCHAR(100). ImageField ,就数据库而言,是一个简单的 VARCHAR(100)。 that means you can only store up to 100 characters in it.这意味着您最多只能在其中存储 100 个字符。 Most of the "magic" of ImageField is that it provides an interface to store the file in the MEDIA_ROOT with particular paths and provides tools to retrieve it again. ImageField的大部分“魔力”在于它提供了一个接口以将文件存储在具有特定路径的MEDIA_ROOT中,并提供了再次检索它的工具。 It also provides tools to store height and width of the image.它还提供了存储图像高度和宽度的工具。

If you really want to store binary files in the database, you'll have to create your own system using a BinaryField or something like that.如果您真的想在数据库中存储二进制文件,则必须使用BinaryField或类似的东西创建自己的系统。 Remember that you'll likely also need to store some meta information such as filename or mimetype (since the raw binary won't have that).请记住,您可能还需要存储一些元信息,例如文件名或 mimetype(因为原始二进制文件没有这些信息)。

In most cases, storing binary files in a database is usually not the best solution as it'll be slower than just fetching files from a file system.在大多数情况下,将二进制文件存储在数据库中通常不是最佳解决方案,因为它比仅从文件系统中获取文件要慢。 If you want the files to be accessible between machines, then you'd be better with a network mount or something like AWS S3.如果您希望文件可以在机器之间访问,那么最好使用网络挂载或 AWS S3 之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM