0

I have a html file that contains the following line:

core.html
{% include 'events/events.html' %}

And I have this other template:

events.html
{% for event in events %}
    {{ event.event_name }} <br/>
{% endfor %}

When I open the URL that loads the events.html it perfectly shows the data:

Evento 1 
Pentaho Workshop

But when I open the main URL that loads core.html it shows me nothing. If I write "blabla" out of the for block it is shown!!!

Events -> views.py

from django.shortcuts import render
from models import Events

def events_index(request):
    events = Events.objects.all()
    return render(request, "events/events.html", locals())

Core -> views.py

from django.shortcuts import render

def core_index(request):
    return render(request, "core/core.html", locals())

Could anyone help me?

Lucas Rezende
  • 2,516
  • 8
  • 25
  • 34

1 Answers1

1

When you navigate to the URL for events.html that invokes the events_index view which loads the events object into the template context via events = Events.objects.all(). The core_index view doesn't include events in the context, so there's nothing for the loop to process. If you add an import for the Events model and events = Events.objects.all() to core_index things will work more like you expect.

ob1quixote
  • 399
  • 1
  • 3
  • 8
  • It worked! Are there anyway to put all the logic and process within the app to reuse it? The way I was expecting it to work is the idea I want to express... is there any way of doing it? – Lucas Rezende May 14 '14 at 01:27
  • 1
    I think I understand what you want, and you _can_ include a view in another view, for some value of include, _q.v._ ["Can I call a view from within another view?"](http://stackoverflow.com/questions/4808329/can-i-call-a-view-from-within-another-view). However, I don't think I would. If you need to reference the Events model in both views, so be it. Views should be compositions of models and the necessary logic to return the rendered HTML you need. I think it's okay to reference the same model in more than one view. – ob1quixote May 14 '14 at 01:42