gpt4 book ai didi

python - django 如何处理二进制 post 数据?

转载 作者:行者123 更新时间:2023-12-01 05:50:31 24 4
gpt4 key购买 nike

post data

user_logo文件实际上是一个img数据,

如何将此 img 保存到 django 模型?

最佳答案

models.py

class Image(models.Model):
image = models.ImageField(upload_to='image_uploads')
created = models.DateTimeField(auto_now_add=True)

forms.py

class ImageForm(ModelForm):
class Meta:
model = Commercial

views.py

def add_image(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return direct_to_template(request, 'page.html', {
'form': ImageForm()
})

page.html

<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.image}}
</form>

关于python - django 如何处理二进制 post 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com