0

I have such problem: From django admin I'm trying to upload a cover image for my article, but image doesn't show on template, and its name is written in red like in this picture in PyCharm. Here are my codes:

settings.py:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

models.py:

class Article(models.Model):
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=200)
    content = HTMLField()
    # image = models.CharField(max_length=2000)
    image = models.ImageField(upload_to='static/img')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    post_date = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(unique=True, null=True, editable=False)

    def __str__(self):
        return self.title

article.html:

<div class="blog-item wide-blog-item">
  <a href="/"><img src="/{{ article.image }}" class="image"/></a>
  <h3 class="font-reg"><a href="">{{ article.title }}</a></h3>
  <div class="blog-item-meta">
  <span>{{ article.post_date }}</span>
  </div>
  <div class="blog-item-meta">
  <span style="text-transform: capitalize;">{{ article.category }} . </span>                            
  </div>
</div>

views.py:

def index(request):
    context = {
        'category_list': Category.objects.all(),  # navbarda gostermek uchun
        'articles': Article.objects.all(),
    }
    return render(request, 'articles.html', context)


def article(request, article_slug, article_category):
    try:
        context = {
            'article': Article.objects.get(slug=article_slug),
            'category_list': Category.objects.all(),  # navbarda gostermek uchun
            'category': Category.objects.filter(name=article_category),  # url-de category name gostermek uchun
        }
    except Article.DoesNotExist:
        raise Http404("Such article could not be found")
    return render(request, 'article.html', context)
srvqaqa
  • 442
  • 1
  • 5
  • 18
  • Have you tried `src="{{ article.image.url }}"`? – Daniel Holmes Aug 23 '19 at 11:26
  • @DanielHolmes didn't help, man – srvqaqa Aug 23 '19 at 11:30
  • 1
    Adding to what user11418935 mentioned above, you should NOT use your applications static file folder for the user media uploads. You can refer to Django documentation for [file upload handlers][1] and [file management][2] for proper storage of your media uploads. Hope this helps! [1]: https://docs.djangoproject.com/en/2.2/topics/http/file-uploads/ [2]: https://docs.djangoproject.com/en/2.2/topics/files/ – devdob Aug 23 '19 at 11:33
  • @devdob I can't understand, man, please help me. Now I must create new folder ? But where ? With the name of what ? In documentation its written with forms, my application isn't written with forms – srvqaqa Aug 23 '19 at 11:38
  • The application will take care of the folder creation. What user11418935 told you in his answer below is correct. Add the extra URL to the url patterns and settings for MEDIA and you should be good to go. – devdob Aug 23 '19 at 11:47

0 Answers0