Problem with For and Forloop

31 views
Skip to first unread message

Bak4nimE Oficial

unread,
Oct 31, 2020, 9:25:18 AM10/31/20
to Django users
Hello good, I have a problem using the for and for loop, what I want is that this process is repeated in all pages, so I put it in the base page.html. I want the last added in all pages like, in the blog, login and index. At this moment you can only see it in the index.

This is code inside the base:
<div>
                    <h3 class="blog-title">Latest Additions</h3>
                    {% for anime in num_blog %}
                    {% if forloop.revcounter %}
                    <a href="{% url 'anime-detail' anime.pk%}">
                        <img class="img-asides" src="{{ anime.image.url }}" alt=""></a>
                    <h3
                        style="text-align: center; max-width: 180px; font-size: 13px; margin-right: auto; margin-left: auto;">
                        {{ anime.tituloPag }}</h3>
                    {% endif %}
                    {% endfor %}
                </div>

This is the code in views.py:
def index(request):
    num_blog = Anime.objects.all()
    
    return render(
        request,
        'index.html',
        context={'num_blog':num_blog}
    )

def blog(request):
    num_blog = Anime.objects.all()

    return render(
        request,
        'blog.html',
        context={'num_blog':num_blog}
    )

But as I said you can only see the loop inside the index.html and not in the other pages, what can I do?
Reply all
Reply to author
Forward
0 new messages