On Mon, Aug 8, 2011 at 4:36 PM, Josh <jos.carpente...@yahoo.com> wrote:
> > >> >> >If you need to push back a value in a variable call related_entries, then >>> you will want to set a value in the context dictionary: >> >> > >> >> >context[related_entries] = <some value here> >> >> > Correction on earlier mail when trying some other things. I do this in the > def render. This looks like: > > class RelatedEntryNode(template.Node): > > def __init__(self, model, number, categories, varname): > self.model = model > self.number = int(number) > self.categories = template.Variable(categories) > self.varname = varname > Ok, so this means that: self.model is a string; literally, in your example, "weblog.entry" self.number is an integer self.categories is a variable reference -- you can resolve it at render time, to obtain the value of object.categories (or, depending on what object happens to be, Django could try object['categories'], or object.categories(), or a few other things -- the important thing is that the Django template framework will do the work of getting you a value) self.varname is another string; in this case, "related_entries" Now, I *think* that you want self.model to be a Variable as well, just like self.categories, because I *don't think* that you are actually interested in the string "weblog.entry" -- I think that you want the value of the entry attribute of the weblog object. You want Django to resolve that value for you at template render time. > > def render(self, context): > related = self.categories.resolve(context) > context[self.varname] = related > return '' > > > > >> >> >You won't be able to resolve it as a Variable, because it (presumably) >>> doesn't even exist >before you call your custom tag, but once you set it on >>> the context object, it will be >available to the rest of the template. >> >> >>> This I don't understand. As I understand Django (but correct me if I'm >>> wrong) the related_entries should be a dictionary or list of >>> Entry-instances. >> >> >> That depends entirely on what *you* want *your template tag* to do. Given all of your descriptions, though, I'm led to believe that you want your tag to create a new variable, which you can use in your template. So, when you put {% get_related_entries weblog.entry 5 from object.categories as related_entries %} in your template, {{related_entries}} doesn't exist as a variable *before* your tag, but *after* your tag, it has some value, which you will have computed in the render() method. In that case, then *in your node class*, self.related_entries is just a string -- the name of the variable to create. In your example, it happens to be the string "related_entries", but you could have written {% get_related_entries weblog.entry 5 from object.categories as cheeseburger_hotel %} and then self.related_entries would contain the string "cheeseburger_hotel". But that's fine, because your render() method is going to compute some value, probably a list of Entry instances in this case, and then it will create a new context variable, with the statement; context[self.related_entries] = <my_previously_computed_list> And that is what is going to create the context variable {{related_entries}}, or {{cheeseburger_hotel}}, or whatever the template author asked for. I hope that helps -- I think that you may be confusing things by using the same names for your python variables as you do for your template variables, when they really are very different things. -- Regards, Ian Clelland <clell...@gmail.com> -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.