Content using {% include %} not appearing on detailed page Django

867 views
Skip to first unread message

Andrew Nguyen

unread,
Dec 16, 2014, 2:27:41 PM12/16/14
to django...@googlegroups.com

I'm having some problems getting some of my content to appear on my detailed page view, which I have in two separate html files that are being pulled in using {% include %}. Inside my two files, slider.htmland sidebar.html, I'm using tags like {{article.title}} to grab specific information I need about an article.

TRIED: 
- Changing syntax in the two included files to {{object.title}} did not work.

- I've tried doing {% include "sidebar.html" article=object %} in my detailed.html and it did not work. Instead, I would get an error TemplateSyntaxError at /gone-home Unknown argument for u'include' tag: u'article=object'.

- Also tried doing `{% include "sidebar.html" with article=object %} did not work either.

detailed.html

<!-- This grabs the sidebar snippet -->
{% include "sidebar.html" %}

<div class="mainContent clearfix">
    <h1>{{object.title}}</h1>
    <p class="date">{{object.pubDate|date:"l, F j, Y" }}</p> | <p class="author">{{object.author}}</p>
    <img src="{{object.heroImage}}" alt="" class="largeImage">
    <div class="contentBlock">
        <img src="{{object.relatedImage}}" alt="" class="relatedImage">

        <p class="content">{{object.body|linebreaks}}</p>
    </div><!-- /.contentBlock -->

    <!-- This grabs the slider snippet -->
    {% include "slider.html" %}

</div><!-- /.mainContent -->

slider.html

<div class="slider">
            <div class="group visible">
                <div class="sliderItem">
                    <a href="{%url "detailed" slug=article.slug %}"><img src="{{article.relatedImage}}" alt="" class="sliderPicture"></a>
                    <a href="{%url "detailed" slug=article.slug %}"><p class="related">{{article.title}}</p></a>
                </div><!-- /.sliderItem -->
            </div><!-- /.group -->
    </div><!-- /.slider -->

sidebar.html

<div class="navContent">
            {% for article in object_list|slice:":5" %}
            <div class="navItem">
                <div class="overlay">
                </div><!-- /.overlay -->
                    <a href="{%url "detailed" slug=article.slug %}"><img src="{{article.relatedImage}}" alt="" class="navPicture"></a>
                <a href="{%url "detailed" slug=article.slug %}"><p class="navTitle">{{article.title|truncatewords:"4"}}</p></a>
            </div><!-- /.navItem -->
            {% endfor %}
        </div><!-- /.navContent -->

models.py

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class FullArticleQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class FullArticle(models.Model):
    title = models.CharField(max_length=150)
    author = models.CharField(max_length=150)
    slug = models.SlugField(max_length=200, unique=True)
    pubDate = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    category = models.CharField(max_length=150)
    # gameRank = models.PositiveSmallIntegerField(default=0)
    heroImage = models.CharField(max_length=250, blank=True)
    relatedImage =  models.CharField(max_length=250, blank=True)
    body =  models.TextField()
    publish = models.BooleanField(default=True)

    objects = FullArticleQuerySet.as_manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("FullArticle_detailed", kwargs={"slug": self.slug})

    def random(self):
        return self.get_queryset().order_by('?').values('title','author','heroImage','body').first()

    class Meta:
        verbose_name = "Blog entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-pubDate"]

views.py

from django.views import generic
from . import models 

# Create your views here.
class BlogIndex(generic.ListView):
    queryset = models.FullArticle.objects.published()
    template_name = "list.html"
    # paginate_by = 2

class BlogDetail(generic.DetailView):
    model = models.FullArticle
    template_name = "detailed.html"

Fred Stluka

unread,
Dec 16, 2014, 11:07:44 PM12/16/14
to django...@googlegroups.com
Andrew,

I'm not sure, but since no one else has answered yet...

Maybe you can't pass an object as a keyword argument to include,
can only pass strings?  I've done it often with strings, but never
with an object.  Try:

    {% include "sidebar.html" title=object.title %}

and in the included file, use simply:

    {{title}}

It seems odd that there'd be a restriction like this since I'm sure
you can pass objects to templates from views, but it's worth
trying.  May be a good workaround, or may get you a different
error message that tips you off to what the real problem is.

--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
On 12/16/14 2:27 PM, Andrew Nguyen wrote:

I'm having some problems getting some of my content to appear on my detailed page view, which I have in two separate html files that are being pulled in using {% include %}. Inside my two files, slider.htmland sidebar.html, I'm using tags like {{article.title}} to grab specific information I need about an article.

TRIED: 
- Changing syntax in the two included files to {{object.title}} did not work.

- I've tried doing {% include "sidebar.html" article=object %} in my detailed.html and it did not work. Instead, I would get an error TemplateSyntaxError at /gone-home Unknown argument for u'include' tag: u'article=object'.

- Also tried doing `{% include "sidebar.html" with article=object %} did not work either.

detailed.html

<!-- This grabs the sidebar snippet -->
{% include "sidebar.html" %}

<div class="mainContent clearfix">
    <h1>{{object.title}}</h1>
    <p class="date">{{object.pubDate|date:"l, F j, Y" }}</p> | <p class="author">{{object.author}}</
p<
/span>>

    slug = models.SlugField(max_length=200, unique!
 =True)

    pubDate = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    category = models.CharField(max_length=150)
    # gameRank = models.PositiveSmallIntegerField(default=0)

    heroImage = models.CharField(max_length=250, b!
 lank=True)
    relatedImage =  models.CharField(max_length=250, blank=True)

    body =  models.TextField()
    publish = models.BooleanField(default=True)

    objects = FullArticleQuerySet.as_manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("FullArticle_detailed", kwargs={"slug": self.slug})

    def random(self):

        return self.get_queryset().order_by('?').valu!
 es<
span class="pun" style="font-size: 13.6960000991821px; background: transparent;">('title','author','heroImage','body').first()


    class Meta:
        verbose_name = "Blog entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-pubDate"]

views.py

from django.views import generic
from . import models 

# Create your views here.
class BlogIndex(generic.ListView):
    queryset = models.FullArticle.objects.published()
    template_name = "list.html"
    # paginate_by = 2

class BlogDetail(generic.DetailView):
    model = models.FullArticle
    template_name = "detailed.html"
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/343cde15-36ab-4432-836b-5947922e48b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Roseman

unread,
Dec 17, 2014, 4:29:39 AM12/17/14
to django...@googlegroups.com
On Tuesday, 16 December 2014 19:27:41 UTC, Andrew Nguyen wrote:

I'm having some problems getting some of my content to appear on my detailed page view, which I have in two separate html files that are being pulled in using {% include %}. Inside my two files, slider.htmland sidebar.html, I'm using tags like {{article.title}} to grab specific information I need about an article.


But your code shows that you are doing something completely different: you're not grabbing specific information about the article in question, you're trying to get details for *all* articles and iterate through them. Your detail view presumably doesn't passs any reference called `object_list`, so your loop doesn't work. Passing in parameters to include won't help, because you don't have the information in the first place.

What you really need here is a custom inclusion tag (https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/#inclusion-tags) which queries the object_list and passes it to the sidebar template for rendering.

--
DR.

Reply all
Reply to author
Forward
0 new messages