2

I'm going through the Django tutorial right now (https://docs.djangoproject.com/en/3.0/intro/tutorial04/) and encountering this little problem with pluralize.

With this code:

<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{

The pluralize filter isn’t doing anything, The template returns something like this:

Just hacking again -- 2 vote{{ votes|pluralize }} // just the template code itself.

as if it was just ignoring the {{ }} indicators.

If I take pluralize out, then the choice.votes variable will display (in this case) a 2, as expected, just like it does with {{ choice.votes }}. The addition of | pluralize seems to break the interpolation, just in that area.

I don't see any sort of import or the like that I need to add; I even copy and pasted to ensure no spelling errors, nor do I see anything in the console.log.

Any insight into what might be (not) happening here?

3
  • try without the spaces: vote{{ choice.votes|pluralize }}
    – Ben
    Aug 11, 2020 at 17:06
  • @Ben, thanks. I think the actual sample had no spaces either; still no go even w/o spaces. I'll update the post for clarity there. Aug 11, 2020 at 17:30
  • Oh, wait! It did work! My autoformat tool was adding spaces between the {{ <stuff> }} and that caused the problem. Should look like this, actually: vote{{choice.votes|pluralize}} Aug 11, 2020 at 17:34

2 Answers 2

4

@Ben provided the start of the solution by suggesting removing spaces, like this:

vote{{ choice.votes|pluralize }} // not quite

That was close. I took out all spaces (which had been added in my auto-formatter because of where my lines split) and this worked:

vote{{choice.votes|pluralize}}   // bingo
3
  • This works too: vote{{votes | pluralize}} (spaces around the |, but still not before/after the {{ }}). Aug 11, 2020 at 17:38
  • Which I find odd, because {{ choice.choice_text }} works fine with interpolation (despite spaces next to the braces). Could there be something deeper missing that applies to the Django template filters only? For now, the immediate problem has at least been solved. Aug 11, 2020 at 17:47
  • You're definitely going to want to take a closer look at what your autoformat tool is doing, because the spaces inside -- next to -- the double-braces should work as pasted from the tutorial sample code: docs.djangoproject.com/en/3.0/intro/tutorial04/#id5
    – Ben
    Aug 14, 2020 at 19:36
0

To me what actually solved this problem was writing all on the same line:

vote{{ choice.votes|pluralize }}

And this worked regardless of the above-mentioned spaces in between.

The code in Django's tutorial (https://docs.djangoproject.com/en/3.2/intro/tutorial04/) shows a line break right after the opening {{, so it can be misleading: enter image description here

(Screenshot taken in Chrome at 100% zoom. But this line break also shows up until 125%)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.