0

This is my template code:

{% block content %}
    {% get_latest as latest_posts %}
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

{% block sidebar %}    
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

In the content block the for loop does works, but in the sidebar block I can't use the variable latest_posts. Can anyone help me with this?

2 Answers 2

4

The variable's scope is limited to the containing {% block content %}. You can either repeat the declaration inside {% block sidebar %}, or move it up a level so it's outside of {% block content %}.

example

{% get_latest as latest_posts %}

{% block content %}
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

{% block sidebar %}    
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

or

{% block content %}
    {% get_latest as latest_posts %}
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

{% block sidebar %}  
    {% get_latest as latest_posts %}  
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried with your first sollution, but doesn't work in both blocks :( and in your second example, i don't wanna do 2 sql queries, any other solution?
2

You can use the with statement:

Definition:

with Caches a complex variable under a simpler name. This is useful when accessing an “expensive” method (e.g., one that hits the database) multiple times.

Wrap your existing block into this with clause and you can safe yourself some queries. also it should solve your problem :-) Just remove the {% get_latest as latest_posts %} line

{% with latest_posts=get_latest %}

{% block content %}
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}

{% block sidebar %}    
    <ul>
    {% for post in latest_posts %}
        <li>
            <p><a href="{{ post.get_absolute_url }}">{{ post.title|safe }}</a></p>
        </li>
    {% endfor %}
    </ul>
{% endblock %}
{% endwith %}

1 Comment

This is good, now i have another problem, my template tag is now {% get_latest_posts 6 'category' as latest_posts %}, how can i use this with with statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.