模型.py
class Report(models.Model):
user = models.ForeignKey(User, null=False)
photo_files_attached = models.BooleanField('Photos', default=False)
表格.py
class MediaForm(forms.ModelForm):
photo_files_attached = forms.FileField(label='Choose a file')
class Meta:
model = Report
field = ['photo_files_attached']
视图.py
def media(request):
user = request.user
try:
report = Report.objects.get(user=user.id)
except:
report = None
mediaForm = MediaForm()
if request.method =='POST':
mediaForm = MediaForm(request.POST,request.FILES)
if mediaForm.is_valid():
media = Report(photo_files_attached = request.FILES['photo_files_attached'])
media.save()
return render(request, 'media.html',
{
'mediaForm':MediaForm,
})
我正在尝试通过 django 上传图像文件并将其保存在数据库中。文件保存后,图像将被收集到项目的媒体文件夹中。但它完全不起作用。我知道我做错了什么。