Django 图片上传无法正常工作
Django Image Upload Not Working Properly
我的数据库正在更新图像名称和路径,但图像尚未上传到指定文件夹。我是 Django 的新手,如果我的编码有误,请提供帮助。下面是我的代码:
Model.py:
class MasterBank(models.Model):
bankid = models.BigIntegerField(primary_key=True,blank=True)
bankname = models.CharField(max_length=255, blank=True)
banklogourl = models.ImageField(upload_to ='images/', blank=True)
views.py :
class imageUpload(APIView):
def post(self,request):
checkReq = request.FILES['banklogourl']
m = MasterBank.objects.get(pk=1)
m.banklogourl = checkReq
m.save()
return HttpResponse('image upload success')
Urls:
url(r'^imageupload/$', views.imageUpload.as_view(), name="imageUpload"),
应在 settings.py
中指定媒体路径
MEDIA_ROOT = normpath(加入(SITE_ROOT, 'media'))
MEDIA_URL = '/media/'
以上是模型中指定的文件夹中存放图片的媒体文件夹。
我的数据库正在更新图像名称和路径,但图像尚未上传到指定文件夹。我是 Django 的新手,如果我的编码有误,请提供帮助。下面是我的代码:
Model.py:
class MasterBank(models.Model):
bankid = models.BigIntegerField(primary_key=True,blank=True)
bankname = models.CharField(max_length=255, blank=True)
banklogourl = models.ImageField(upload_to ='images/', blank=True)
views.py :
class imageUpload(APIView):
def post(self,request):
checkReq = request.FILES['banklogourl']
m = MasterBank.objects.get(pk=1)
m.banklogourl = checkReq
m.save()
return HttpResponse('image upload success')
Urls:
url(r'^imageupload/$', views.imageUpload.as_view(), name="imageUpload"),
应在 settings.py
中指定媒体路径MEDIA_ROOT = normpath(加入(SITE_ROOT, 'media')) MEDIA_URL = '/media/'