mardi 4 août 2015

Django pass render_to_response template in other template

this is probably a question for absolute beginners since i'm fairly new to progrmaming. I've searched for couple of hours for an adequate solution, i don't know what else to do.

Following problem. I want to have a view that displays. e.g. the 5 latest entries & 5 newest to my database (just an example)

#views.py
import core.models as coremodels

class LandingView(TemplateView):
    template_name = "base/index.html"

    def index_filtered(request):
        last_ones = coremodels.Startup.objects.all().order_by('-id')[:5]
        first_ones = coremodels.Startup.objects.all().order_by('id')[:5]
        return render_to_response("base/index.html", 
        {'last_ones': last_ones,   'first_ones' : first_ones})  

Index.html shows the HTML content but not the content of the loop

#index.html

<div class="col-md-6">
    <p> Chosen Items negative:</p>
    {% for startup in last_ones %}
        <li><p>{{ startup.title }}</p></li>
    {% endfor %}
</div>

<div class="col-md-6">
    <p> Chosen Items positive:</p>
    {% for startup in first_ones %}
       <li><p>{{ startup.title }}</p></li>
    {% endfor %}

Here my problem:

How can I get the for loop to render the specific content?

I think Django show render_to_response in template comes very close to my problem, but i don't see a valid solution there.

Thank you for your help.

Chris

-- I edited my code and problem description based on the solutions provided in this thread



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire