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