我一直在尝试使用 Django 的上传功能,但我似乎无法让它工作。我的表格已经包含enctype="multipart/form-data"
,所以这不是问题。无论如何,这就是我正在使用的东西:
这是我的模型
class Photo(models.Model):
""" This represents a Photo
"""
caption = models.CharField(_("caption"), max_length=100, null=True,
blank=True, unique=True)
image = ValidateImageField(
upload_to='uploaded_photos/Published/%Y/%B/%d/',
content_types=['image/jpeg', 'image/gif', 'image/png'],
max_upload_size= settings.FILE_UPLOAD_MAX_MEMORY_SIZE
)
source = models.URLField(_("source"), null=True, blank=True)
status = models.CharField(_("status"), max_length=1, choices=STATUS_CHOICES, default='H')
date_added = models.DateTimeField(_("date_added"), auto_now_add=True)
date_modified = models.DateTimeField(_("date_modified"), auto_now=True)
slug = models.SlugField(_("slug"), max_length=50, unique=True)
extra_info = models.TextField(_("extra_info"), null=True, blank=True)
这是我的表格:
class UploadImageForm(forms.ModelForm):
class Meta:
model = Photo
field = ('image', 'caption', 'source', 'extra_info',)
exclude = ('status','slug',)
这是我的看法:
def submit_image(request):
if request.method == 'POST':
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid:
form.save()
return redirect('photos.views.upload_success')
form = UploadImageForm()
return render(request,'photos/upload.html', {
'form': form
})
我已经尝试过使用 djangodocs 中所示的 chunks 方法,但这也不起作用。我的代码是否有问题,如果有的话,如果有人能指出,我将不胜感激。提前致谢。