python - Can't upload image in django use MEDIA_ROOT and MEDIA_URL -
hello want make upload image in admin django when use media_root , media url image can not upload. model.py
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(), })
and tamplate product.html
<img src="{{post.image.url}}" alt="" />
can me solve problem?
media_root
absolute path uploaded images should change setting this:
media_root = os.path.join(base_dir, 'static/images/upload')
the second problem in image field definition. upload_to
argument path relative media_root
/media_url
.
image = models.imagefield(upload_to='product')
and better add strftime()
formatting reduce number of files in single directory:
image = models.imagefield(upload_to='product/%y/%m/%d')