Thank you Aidas. As it turns out, I already had a foreignkey
relationship with my user, so I didn't need a template tag at all.
Good to know how to do it, though.
On Feb 13, 10:27 am, "Aidas Bendoraitis" <[EMAIL PROTECTED]>
wrote:
> Hello Baxter!
>
> Take a look at my approach. It might help you. The following template
> tag "load_obj" loads any object by id and saves passes it to the
> template context variable:
>
> # ---- code starts here ----
> from django.db.models import get_model
> from django import template
>
> register = template.Library()
>
> def do_load_obj(parser, token):
> try:
> # split_contents() knows not to split quoted strings.
> tag_name, appmodel, object_id, str_as, var_name =
> token.split_contents()
> except ValueError:
> raise template.TemplateSyntaxError, "%r tag requires a
> following syntax: {%% %r app.model <object_id> as <var_name> %%}" %
> (token.contents[0], token.contents[0])
> try:
> appname, modelname = appmodel.split(".")
> except ValueError:
> raise template.TemplateSyntaxError, "%r tag requires
> application name and model name separated by a dot" %
> (token.contents[0], token.contents[0])
> from django.conf import settings
> model = get_model(appname, modelname)
> return LoadObjNode(model, object_id, var_name)
>
> class LoadObjNode(template.Node):
> """ {% load_obj app.name <object_id> as <var_name> %} """
> def __init__(self, model, object_id, var_name):
> self.model = model
> self.object_id = object_id
> self.var_name = var_name
> def render(self, context):
> try:
> object_id = template.resolve_variable(self.object_id, context)
> obj = self.model.objects.get(pk=object_id)
> except ValueError:
> obj = None
>
> context[self.var_name] = obj
> return ''
>
> register.tag('load_obj', do_load_obj)
>
> # ---- code ends here ----
>
> I think, this is a good example of both -- reading a value of a
> variable in a template tag and also setting a value to the context
> variable.
>
> Regards,
> Aidas Bendoraitis aka Archatas
>
> On 2/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
> > I need to pass a user id to a template tag.
> > After reading this:http://groups.google.com/group/django-users/
> > browse_thread/thread/96fe34b4561415dc/b7aba25f22bdd261?
> > lnk=gst&q=template+tags+variable&rnum=1#b7aba25f22bdd261
>
> > I came up with:
>
> > class SongListNode(template.Node):
> > def __init__(self, artist, varname):
> > self.artist, self.varname = artist, varname
>
> > def __repr__(self):
> > return "<Song Node>"
>
> > def render(self, context):
> > artist=self.artist.resolve(context)
> > context[self.varname] = Song.objects.filter(artist=artist)
> > #context[self.varname] = [self.artist]
> > return ''
>
> > class DoGetSongList:
> > """
> > {% get_song_list as song_list %}
> > """
> > def __init__(self, tag_name):
> > self.tag_name = tag_name
>
> > def __call__(self, parser, token):
> > bits = token.contents.split()
> > if len(bits) != 4:
> > raise template.TemplateSyntaxError, "'%s' tag takes three
> > arguments" % bits[0]
> > if bits[2] != "as":
> > raise template.TemplateSyntaxError, "second argument to
> > '%s' tag must be 'as'" % bits[0]
> > #return SongListNode(bits[1], bits[3])
> > return SongListNode(parser.compile_filter(bits[0]), bits[3])
> > register.tag('get_song_list', DoGetSongList('get_song_list'))
>
> > But no love. Suggestions?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---