DjangoDjango·

Django ImageField max_length error when uploading image

投稿時間:2024-06-07 22:05:31閲覧数:342
専門記事
転載は出所を明記してください

I'm using Django 4.2. I have a model whose fields containing ImageField, for example:

class Example(models.Model)
    image = models.ImageField(
          blank=True,
          upload_to=my_image_path,
      )

When I uploaded my image, I got this error from django: Ensure this filename has at most 100 characters (it has 107).

Taken from the Django documentation:

FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.

And note that ImageField is a subclass of FileField:

Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.

ImageField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.

Therefore, we just need to simply add max_length argument into the ImageField like ordinary CharField:

class Example(models.Model)
    image = models.ImageField(
          blank=True,
          upload_to=my_image_path,
          max_length=500
      )

After this, don't forget to update it in your database:

python manage.py makemigrations
python manage.py migrate


コメント欄

まだコメントがありません。最初のコメントを投稿しましょう!

弦圈热门内容

Djano云服务器部署 uwsgi+nginx+https部署

Django启动服务器的命令runserver,其实也是启动了一个自带的uwsgi服务。如果想要一直在后台不停的运行django项目,需要部署uwsgi服务器。uwsgi安装官方文档:安装uWSGI — uWSGI 2.0 文档 (uwsgi-docs-zh.readthedocs.io)可以直接用pip安装uwsgi:pip install uwsgi在django项目根地址,即跟manage.py同目录下新建配置文件uwsgi.ini,如:mysite1/uwsgi.iniuwsgi.ini必须以[uwsgi]开头,内容如下:[uwsgi] chdir=/mysite1 module=mysite1.wsgi:application socket=127.0.0.1:8000 master=True pidfile=uwsgi.pid vacuum=True max-requests=5000 daemonize=uwsgi.logchdir为django项目根地址,socket为django启动服务器的ip地址,daemonize为日志地址。注意,每个django项目中自动生成一 ...

大学毕业转行后的一点想法

最近成功把以前写的PDF格式的数学文章,几乎完美复刻到HTML网页上面,文章中的数学公式使用JS插件Mathjax渲染。之后会陆续更新到网站上,希望以后能让更多人无需下载就能看到,这也算给大学四年一个结尾。链接如下👇👇👇Note on arithmetic algebraic geometry, An introduction to different branches of mathematics, Note on perfectoid spaces, 代数几何简介​然后我目前只会把我以前留下的notes、introduction之类的弄成HTML这样网页的形式。至于我写的论文存arXiv上面就好了,谷歌搜也能搜到我的论文。目前来看,距离我论文完成也过去一年半了,并没有太多人对于推广perfect这一概念感兴趣。但值得一提的是,目前来看,我的工作更加受到老外的欣赏和认可,没有一个中国的Phd给我写过信,说看过我的文章。虽然关于perfect这一系列的工作没有全部完成,还可以继续深入耕耘,说不定还能多产出几篇论文吧,算下来我本科完成了4篇论文,有5篇未完成,总页数超过100页。但这一切 ...

token简介以及python计算token的方法

后端登陆实现需要用到token机制或者cookies机制。Token和cookies都可以用来存放用户信息。但是token可以将信息存放在浏览器中的localstorage中,不占用服务器内存,而cookies则需要在每次请求中送往服务器中,吃服务器资源。同时,token作为一种比cookies更新的技术,有更多的优势,可以参考token和cookie的区别。每个用户都可以对应一个token值。Token可以由用户名+密码+时间,经过哈希加密得到,也可以直接由用户名和密码经过加密算法加密后得到。复杂程度取决于自己。加密后得到的token值,存放在会话session中。Python使用哈希算法进行加密,计算token值:import hashlib md5 = hashlib.md5() md5.update((username+password+"1258"+str(time.time())).encode()) token = md5.hexdigest()也可以使用python的pip库pyjwt来实现加密,计算token值。$ pip install pyjwtJwt输入的是字 ...

为什么有了uwsgi 还要 nginx 服务器?

有关nginx的简单介绍,详见Nginx服务器反向代理。uwsgi是python的一个通信协议,同时也是一种web服务器,而nginx则是高性能反向代理的web服务器。在Django项目服务器部署中,uwsgi几乎是不可替代的。然而部署好了uwsgi,其实django接口已经能够响应请求,为什么还要额外配置nginx服务器?因为,相比于直接将真实地址暴露在公网上,在外面套一层nginx安全性更高,具体如下:安全问题,程序不能直接被浏览器访问到,而是通过nginx,nginx只开放某个接口,uwsgi本身是内网接口,这样运维人员在nginx上加上安全性的限制,可以达到保护程序的作用。负载均衡问题,一个uwsgi很可能不够用,即使开了多个work也是不行,毕竟一台机器的cpu和内存都是有限的,有了nginx做代理,一个nginx可以代理多台uwsgi完成uwsgi的负载均衡。静态文件处理效率问题,用django或是uwsgi这种东西来负责静态文件的处理是很浪费的行为,而且他们本身对文件的处理也不如nginx好,所以整个静态文件的处理都直接由nginx完成,静态文件的访问完全不去经过uwsg ...