Answer the question
In order to leave comments, you need to log in
How to not display an image if it does not exist?
views.py method:
def index(request):
content = News.objects.all()
for el in content:
el.picture = '/' + str(el.picture).split('/')[-1]
context = {
'content' : content
}
return render(request, 'News.html', context)
<body>
<h1> News </h1>
{% for el in content %}
<h1><a href = "/news/{{el.pk}}"> {{ el.title }} </a></h1>
<img src = "{% static 'pictures' %}{{el.picture}}", width="100", height="100">
<p> {{el.content}} </p>
<p> {{el.published}} </p>
<a href = "/news/rubric№{{el.rubric.pk}}"> {{el.rubric}} </a>
{% endfor %}
</body>
{% if el.picture != "" %}
<img src = "{% static 'pictures' %}{{el.picture}}", width="100", height="100">
{% endif %}
{% if ".jpg" in el.picture %}
<img src = "{% static 'pictures' %}{{el.picture}}", width="100", height="100">
{% endif %}
Answer the question
In order to leave comments, you need to log in
Who draws an image like that?
Throw it away, here it is
for el in content:
el.picture = '/' + str(el.picture).split('/')[-1]
{{ el.picture.url }}
{% if el.picture %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question