On Wednesday, 15 October 2014 19:30:01 UTC+1, Daniel Grace wrote: > > Hi, > I'm encountering the following problem with a token in my custom template > tag: > Request Method: GET > Request URL: http://127.0.0.1:8000/flows/ > Django Version: 1.7 > Exception Type: ValueError > Exception Value: > need more than 2 values to unpack > Exception Location: C:\landy\cresta\flow\templatetags\flow_extras.py in > rowcolour, line 7 > > Here is the template file: > from django import template > from datetime import datetime, timedelta > register = template.Library() > > @register.tag > def rowcolour(parser, token): > nm, dt, fmt = token.split_contents() # this is line 7 > diff = datetime.now() - dt > if diff.days > 14: > return "pinkrow" > else: > return "greyrow" > > ... here is the line in the html file: > <tr id="{% rowcolour flow.created %}"> > > ... and "created" is defined as follows in the model: > created = models.DateTimeField(db_index=True, auto_now_add=True) > > I'm trying to extract the datetime from the token. Any ideas? > > Thanks. > > Like the error says, you are telling it to split the result of `token.split_contents()` into three variables, but you are only passing it two elements: "rowcolour" and "flow.created". Where is `fmt` supposed to be coming from?
However, even when you fix that, this still won't work, as at that point `dt` will just be the string "row.created". You need to have a whole template Node in order to resolve variables like that. Why are you not using the `simple_tag` decorator, which is a shortcut for that whole process? @register.simple_tag def rowcolour(dt): diff = datetime.now() - dt ...etc... -- DR. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/8b95004f-1905-4186-b4e4-26f859936777%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.