gpt4 book ai didi

database - Django 表单我无法保存图片文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:41:04 24 4
gpt4 key购买 nike

我有模型:

class OpenCv(models.Model):
created_by = models.ForeignKey(User, blank=True)
first_name = models.CharField(('first name'), max_length=30, blank=True)
last_name = models.CharField(('last name'), max_length=30, blank=True)
url = models.URLField(verify_exists=True)
picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='jakido/avatar',blank=True, null= True)
bio = models.CharField(('bio'), max_length=180, blank=True)
date_birth = models.DateField(blank=True,null=True)
domain = models.CharField(('domain'), max_length=30, blank=True, choices = domain_choices)
specialisation = models.CharField(('specialization'), max_length=30, blank=True)
degree = models.CharField(('degree'), max_length=30, choices = degree_choices)
year_last_degree = models.CharField(('year last degree'), max_length=30, blank=True,choices = year_last_degree_choices)
lyceum = models.CharField(('lyceum'), max_length=30, blank=True)
faculty = models.ForeignKey(Faculty, blank=True,null=True)
references = models.CharField(('references'), max_length=30, blank=True)
workplace = models.ForeignKey(Workplace, blank=True,null=True)

形式:

class OpencvForm(ModelForm):
class Meta:
model = OpenCv
fields = ['first_name','last_name','url','picture','bio','domain','specialisation','degree','year_last_degree','lyceum','references']

和 View :

 def save_opencv(request):
if request.method == 'POST':
form = OpencvForm(request.POST, request.FILES)
# if 'picture' in request.FILES:
file = request.FILES['picture']
filename = file['filename']
fd = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')
fd.write(file['content'])
fd.close()
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.picture = form.cleaned_data['picture']
new_obj.created_by = request.user

new_obj.save()
return HttpResponseRedirect('.')
else:
form = OpencvForm()
return render_to_response('opencv/opencv_form.html', {
'form': form,
},
context_instance=RequestContext(request))

但我似乎没有在我的数据库中保存图片...出了点问题,我不知道是什么:(

最佳答案

你不会轻易帮助你 - 你没有描述实际发生的事情。

但是我的猜测是您没有在 HTML form 元素中包含 enctype="multipart/form-data"

我会推荐阅读 the file upload documentation - 你在这里手动做一些可以由 Django 为你处理的事情。

关于database - Django 表单我无法保存图片文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884981/

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