0
你好我想在管理员django上传图像,但是当我使用media_root和媒体url图像无法上传。这是model.py无法在django中使用MEDIA_ROOT和MEDIA_URL上传图像
class Product(models.Model):
category = models.ForeignKey('Category')
userprofile = models.ForeignKey('UserProfile')
title = models.CharField(max_length=50)
price = models.IntegerField()
image = models.ImageField(upload_to=settings.MEDIA_ROOT)
description = models.TextField()
created_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title;
setting.py
MEDIA_ROOT = '/static/images/upload/'
MEDIA_URL = '/upload/'
view.py
def home(request):
posts = Product.objects.filter(created_date__isnull=False)
return render(request, 'kerajinan/product_list.html', {
'posts' : posts,
'categories' : Category.objects.all(),
})
,这是tamplate product.html
<img src="{{post.image.url}}" alt="" />
你能帮助我解决这个问题?
感谢其工作,但如何显示像这样图像标签img的图像不能加载。怎么样? – User0511
以下是文档:https://docs.djangoproject.com/en/1.8/howto/static-files/#serving-files-uploaded-by-a-user-during-development – catavaran