в settings.py
MEDIA_ROOT = os.path.join(BASE_DIR,'pictures')
MEDIA_URL = '/pictures/'
в models.py
class Campaign(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
# this is many to one relationship, on_deleting user, profile will also be deleted
campaign_title = models.CharField(max_length=200, blank=True)
campaign_image = models.ImageField(default="default_campaign.png",upload_to="campaign_pictures")
в views.py
def landing_page(request):
campaigns = Campaign.objects.all().order_by('-id')
print(campaigns)
return render(request, 'core/landing_page.html',{'campaigns':campaigns})
на целевой странице. html
{% for campaign in campaigns %}
<img src="{{campaign.campaign_image.url}}">
{% endfor %}
выпуск
- если имя файла ab c xyz.jpg, оно сохраняется в / pictures / campaign_pictures как abc_xyz.jpg
- в html шаблоне, sr c изображения должно быть '/pictures/campaign_pictures/abc_xyz.jpg', но оно показывает только '/pictures/abc%20xyz.jpg'