3

I've got a simple view, form and 2 templates in my django (1.9.2) project The template that view is passed works like a charm, iterates through and displays wanted value without problems.

Yet when I want to include this template into another one the iteration does not occur. I've tried using {% include with %} but maybe I'm not doing it right.

The template that is the for the homepage is placed in project template folder, while the template to be included is inside the app

news/models.py:

class News(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateField(db_index=True, auto_now_add=True)

def __unicode__(self):
    return '%s' % self.title

news/views.py:

from news.models import News
from django.shortcuts import render
from django.template import RequestContext

def news(request):
    posts = News.objects.all()
    return render(request, 'news.html',{'posts':posts })

news/templates/news.html:

{% load i18n %} 
{% block content %}
<h2>News</h2>
    :D
    {% for post in posts %}
        {{ post.title }}
        {{ post.body }}
    {% endfor %}
{% endblock content %}

templates/home.html:

{% extends "base.html" %}
{% load i18n %}
{% block content %}
<section id="portfolio">
    <div class="container">

  {% include "news.html" with posts=posts %}

    </div>
</section>
{% include "footer.html" %}
{% endblock content %}

When checking at http://127.0.0.1:8000/news/ everything is fine, but at http://127.0.0.1:8000/ only :D is displayed

No idea how to tackle this Thanks :^)

EDIT:

For the Home I actually use only template and in urls it looks like this:

url(r'^$', TemplateView.as_view(template_name='pages/home.html') ,  name="home")

Also for the base I use cookie-cutter django from cookiecutter-django

Should I define view somewhere for home also?

pydanny
  • 7,954
  • 6
  • 34
  • 42

1 Answers1

2

It seems that when you are using

url(r'^$', TemplateView.as_view(template_name='pages/home.html') ,  name="home")

You are not defining posts at all. To make it working you have to pass context with posts to this name="home" view but you are using default as_view which doesn't pass posts.

I would make it like so:

news/urls.py:

url(r'^$', views.home,  name="home")

news/views.py:

from news.models import News
from django.shortcuts import render
from django.template import RequestContext

def home(request):
   posts = News.objects.all()
   return render(request, 'home.html', {'posts':posts })

news/templates/news.html:

{% load i18n %} 
{% block inner_content %}
<h2>News</h2>
    :D
    {% for post in posts %}
    {{ post.title }}
    {{ post.body }}
    {% endfor %}
{% endblock inner_content %}

templates/home.html:

{% extends "base.html" %}
{% load i18n %}
{% block content %}
<section id="portfolio">
    <div class="container">

  {% include "news.html" with posts=posts %}

    </div>
</section>
{% include "footer.html" %}
{% endblock content %}
omikron
  • 2,745
  • 1
  • 25
  • 34