Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Customizing Django Templates Building Custom Filters Pluralize

Lewis Cowles
Lewis Cowles
74,902 Points

I am sure the elf / elves challenge is broken, am I missing something?

{{ num_elf }} el{{ num_elf | length | pluralize : "f,ves" }}

to me according to the docs on pluralize seems to make sense

because we are forced to write elf, the only way I can see this working is an if statement

code_challenges/templates/code_challenges/list.html
{{ num_elf }} elf{{ num_elf | length | pluralize : "f,ves" }}

4 Answers

Lewis Cowles
Lewis Cowles
74,902 Points

Code that eventually worked... (I don't know why it works exactly, but it does work...)

{{ num_elf }} elf{{ num_elf|pluralize: "elf,elves" %}. {% if num_elf > 5 %}Wow, that's a lot of elves!{% endif %}

Seems mad that I had to supply both words

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You don't need |length since num_elf is a number and not an iterable. You also shouldn't have to supply both names. You should be able to do el{{ num_elf|pluralize:"f,ves" }}.

Lewis Cowles
Lewis Cowles
74,902 Points

Thx Kenneth, I bet it's dropping the filter length that made it work, as the length of a scalar is surely 1... Doh, real homer simpson moment... :smile:

UPDATE

{{ num_elf }} el{{ num_elf|pluralize:"f,ves" }}

Works also, looks like a whitespace issue within the Django tag... The answer I gave works, it's maybe test related as the output is the same, and now I understand pluralize is not being weird, tests are being weird, and nobody but me likes so much whitespace...

Just to let you know the last challenge of the course ("Conjugation Tags") continues on with the elves.

So if you get past tasks 1,2,3 with

@register.simple_tag
def conjugate_is(int):
  if int == 1: 
    return "is"
  elif int != 1: 
    return "are"

You end of dealing with this code given in

"code_challenges/templates/code_challenges/list.html"

{% load fairy_extras %}

There is {{ num_elf }} el{{ num_elf|pluralize:"f,ves" }}.

EEk Ghad! The Elves are back again!! :stuck_out_tongue_closed_eyes:

Of course I got a Bummer! when tried what seemed obvious to me:

{% load fairy_extras %}

There {{ conjugate_is(num_elf) }} {{ num_elf }} el{{ num_elf|pluralize:"f,ves" }}.

Eventually I found this StackOverFlow thread:

http://stackoverflow.com/questions/6451304/django-simple-custom-template-tag-example

...which had this line:

data-rateit-value="{% get_rate crit rates %}"

Based on that line the pattern seemed obvious:

{% load fairy_extras %}

There {% conjugate_is num_elf %} {{ num_elf }} el{{ num_elf|pluralize:"f,ves" }}.

The only other challenge of this course that thwarted me for a time was task 2 of 3 for the first challenge of the course ("Blocks and Inheritance") .

I really felt that this was what was wanted/needed:

{% extends "layout.html" %}

..based on the "home.html" code at 02:36 of the video previous to the first challenge:

https://teamtreehouse.com/library/customizing-django-templates/template-tags-and-filters/template-inheritance

It has that exact line -- why wasn't it working?!

Eventually, through sheer trial and error, I came up with:

{% extends "code_challenges/layout.html" %}

I still don't understand why that was right and the other line in the video was wrong. :confused:


Now I've completed both the Django Basics course and the Customizing Django templates courses.

Got some strange point anomalies though:

1.) Django Basics is supposed to have only 510 total completion points, but I ended up with 564

2.) Customizing Django templates was supposed to have 263 total completion points,

but I actually ended up with 233 --yet the green bar is filled all the way across:

alt text

Hmmm...

Anyway, with them both in the rear view mirror, though, I'm really looking forward to an extended period with no more Django courses.


It's not just python/django are particularly boring...it's all the text being used to generate nothing but text that's a real downer. :anguished:

I know python can generate svg graphics on the fly:

http://code.activestate.com/recipes/325823-draw-svg-images-in-python/

I guess Django can server up svg graphics also if you nail down the mimetypes correctly:

http://stackoverflow.com/questions/2312714/can-the-django-dev-server-correctly-serve-svg

There is even a Django MMORPG:

http://code.google.com/p/django-mmo/

The question is:

will there ever be any Treehouse courses that explorer along those lines..?

Lewis Cowles
Lewis Cowles
74,902 Points

You can always revisit the video's to get more info I guess... I still have not completed Tacocat because I keep re-hashing the video's so I can go over what I would do (benefits of interactive courses I guess)

Can I ask why you would produce dynamic SVG's?

Surely this is the domain of front-end tools like D3, and raphael.js as it offloads the processing to the client...

Anyway, thanks for such in-depth comments

This thread seem to be aimed at just task 3, but Lewis' answer in green satisfies both tasks 3 & 4.


Here's something I noticed though --it's been about a week since the Customizing Django Templates course came out and this is the only forum thread for any of the sections of the Customizing Django Templates course.

That probably means either:

1.) A lot of people might be struggling with Kenneth Love's Django Basics course (especially the final TDD challenge).

So possibly they haven't gotten around to the Customizing Django Templates course..for which Django Basics is a pre-cursor.

2.) Or they are sailing through the Customizing Django Templates course without needing any help

(maybe because this course seems relatively easy when compared to Kenneth Love's Django Basics course)

I guess only time will tell which is these is more probably the case..

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

My answer most certainly does work as I just passed with it.

my answer

Lewis Cowles
Lewis Cowles
74,902 Points

Dude, maybe nobody else made my mistake of length being piped in like I did...

Kenneth's answer does work, (sorry Kenneth) it did make me smile to see your response below, so I've upvoted this just for the smile it put on my face.

Marius Wallin
Marius Wallin
11,464 Points

Can anyone teach my why I need {% extends "code_challenges/layout.html" %} instead of "layout.html" when the file with extend and layout are in the same folder?

And why isn´t this valid? There {% conjugate_is(num_elf) %} but needs to be {% conjugate_is num_elf %}