I've marked a few things that are important I think, because it is getting a bit bulky
>These are my assumptions about the bits of code you've posted: > > > > > >models.py > > >categories = models.ManyToManyField('Category', blank=True, null=True, >> default = None) > > > > > >*Assumption*: categories is a field defined within a standard Django >> model; Category is >another model you have defined. > > >> Yes, categories is a field in model Entry > > >> >> >{% get_related_entries weblog.entry 5 from object.categories as >> related_entries %} > > > {% for entry in related_entries %} > > > <p><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a> > > > {% endfor %} > > > > > > > > >*Assumption:* weblog and object are context variables provided to the >> template from the view. object is an instance of the model with the >> categories m2m field you showed above. > > > > > >I'm not sure how weblog.entry is relevant. > > >> weblog.entry is the model that is used. The templatetag is used when on a >> blogentry detail page and should give a list of related entries (with >> corresponding categories) > > >> It comes from* django.db.models.get_model() * > > (p117 of Practical Django Projects). That's one of the books I'm learning >> from and now I'm trying to understand how custom templatetags work. Already >> made a few and stepping up the complexities > > >> >> >def __init__(self, model, number, categories, varname): > > > self.categories = template.Variable(categories) > > > > > >*Assumption:* model, number, categories, and varname are the parameters >> from the template tag you show in the template snippet above. I am guessing >> that they are all just the strings, exactly as they >appear in the template. > > >> model = weblog.entry > > number = number of entries to display > > categories = object.categories ; these are the categories linked to the >> entry (in this case 3 categories: C1, C2 and C3 ) > > varname = related_entries > > >> >> > > > > > > >def render(self, context): > > > related = self.varname.resolve(context) > > > return related > > > > > >At this point, I presume that you have defined self.varname in __init__ >> above, as "self.varname = varname", and you have just chosen not to paste >> that line. > > > > > >> *This was a typo*, because I was going back to check your suggestion from >> the djangoproject example (which I already tried some time before) > > I corrected this to self.categories.resolve(context) and *I didn't get a >> Template error anymore. But I didn't get any other entries either, and there >> should be at least 1*. > > >> > > > >Caught AttributeError while rendering: 'unicode' object has no attribute >> 'resolve' > > > > > >First off, self.varname, if I am correct, is just the string >> "related_entries" here. It doesn't have a "resolve" method, because it's >> just a string. Variable objects, like self.context, have a "resolve" >> method. > > > > > >If you want to access the categories that you have mentioned in the >> template, then you can use > > >self.categories.resolve(context) > > >> See above. My mistake :-( > > > > > >If you want to access "weblog.entry", then you will need to create a >> Variable from it, like you did with categories: > > > > > >def __init__(...): > > > ... > > > self.model = template.Variable(model) > > > ... > > > > > >and then resolve it in render(), as self.model.resolve(context) > > >> I'll add the Category as a model. Lowercase just as >> django.db.models.get_model() needs it. > > >> self.modelcat = category > > >> The relations are stored in *content_entries_categories* table in the >> Database (id, entry.id, category.id). Django retrieves it while rendering >> the detail_entry template (because it returns the category names in the >> shell. > > >> Basically I want to do the same. Retrieve all entries that have the same >> category.id as the entry on the detailpage. How can I do that in a >> templatetag? > > >> > > > >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> > > > > > >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. > > >> >> > > > >Hope this helps, > > > > > >Ian > > > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6Lra8Zuue2oJ. 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.