django重命名视图中的图像
我想重命名使用模型表单上传的图像,并且我想在视图中执行此操作,因为路径取决于上传它的用户。 到目前为止我有这个:
if request.method == 'POST':
form = AddImageForm(request.POST, request.FILES)
if form.is_valid():
new_image = form.save(commit=false)
但是我如何获取图像,重命名它并保存它。 (也许我还会做一些额外的处理,例如调整大小等) 我对 python 和 Django 很陌生,我不知道如何操作文件。
最好的雅克
I want to rename an image that is uploaded with a Modelform and I want to do this in the view because the path depends on the user who uploaded it.
So far i have this:
if request.method == 'POST':
form = AddImageForm(request.POST, request.FILES)
if form.is_valid():
new_image = form.save(commit=false)
But how do I get the image, rename it and save it. (Maybe I will also do some additional proccessing like resizing, etc.)
I'm quite new to python an Django and I have no clue how to manipulate files.
Best Jacques
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
os 模块允许您与文件系统交互,所有上传文件中的数据都可以在 dict request.FILES 中找到。
Django 文件上传文档
所以也许你会执行 request.files.items()[0][1]
the
os
module lets you interact with the filesystem, and the data in the all the uploaded files is available in the dict request.FILES.Django file upload docs
so maybe you would do
request.files.items()[0][1]