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
the call
render_to_response("base/showlatest.html"...rendersbase/showlatest.html, notindex.html.The view responsible for rendering
index.htmlshould pass all data (last_onesandfirst_ones) to it.Once you have included the template into
index.htmlChange the view above (or create a new one or modify the existing, changing
urls.pyaccordingly) to pass the data to itThe concept is that the view renders a certain template (
index.html), which becomes the html page returned to the client browser. That one is the template that should receive a certain context (data), so that it can include other reusable pieces (e.g.showlatest.html) and render them correctly.The
includecommand just copies the content of the specified template (showlatest.html) within the present one (index.html), as if it were typed in and part of it.So you need to call
render_to_responseand pass it your data (last_onesandfirst_ones) in every view that is responsible for rendering a template that includesshowlatest.htmlSorry for the twisted wording, some things are easier done than explained. :)
UPDATE
Your last edit clarified you are using CBV's (Class Based Views).
Then your view should be something along the line:
Note: personally I would avoid relying on the
idset by the DB to order the records.Instead, if you can alter the model, add a field to mark when it was created. For example
then in your view the query can become