E
E
expp2020-07-03 18:14:13
Django
expp, 2020-07-03 18:14:13

How to render a Django image?

Good evening. How to render an image in Django? I tried to get it out, but to no avail.
The code is presented below. I hope for your help, thank you) (If the question itself is not clear, then you just need to display the movie poster (image) on the site page).

models.py

class Article(models.Model):
    title = models.CharField('Название', max_length=350)
    description = models.TextField('Описание')
    poster = models.ImageField('Картинка', upload_to="articles/")
    date = models.DateTimeField(auto_now_add=True)
    tags = models.ManyToManyField(Hashtags, verbose_name='Теги', related_name='article_tags')
    read_time = models.PositiveSmallIntegerField('Время чтения: ', default='5')
    draft = models.BooleanField('Черновик', default=True)


views.py
class ArticleDetailView(DetailView):
    model = Article
    slug_field = 'title'


urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.ArticleList.as_view(), name='articlelist'),
    path('article/<str:slug>', views.ArticleDetailView.as_view(), name='articledetailview'),

]


HTML Template
<h3>{{ article.title }}</h3>
<img src="{{ article.poster.url }}" alt=''> (Тут картинка, ее не позволяет написать)
<p>{{ article.description }}</p>
<p>Тег:  {{ article.tags }}</p>
<p>Время чтения статьи: {{ article.read_time }}</p>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question