Django custom template tags: 'str' object has no attribute 'notification_set' -
while defining custom template tag acts on top level base.html (not app-level base) running issue query executes fine on shell.
for example,
from django.contrib.auth.models import user u = user.objects.get(pk=3) u.notification_set.filter(viewed=false).count() # returns 1 expected however, in template tag gives me unusual error
attributeerror @ /mynotes/ 'str' object has no attribute 'notification_set' . have manage.py , here custom template tag:
./app/templatetags/mytags.py
from django import template register = template.library() @register.simple_tag(name="unread") def get_unread(user): homecoming user.notification_set.filter(viewed=false).count() ./templates/base.html
{% load mytags %} ... {% unread request.user%} edit: ./app/models.py
class notification(models.model): message = models.textfield() notification_for_user = models.foreignkey(user) viewed = models.booleanfield(default=false) ...
got it. changed
{% unread request.user %} to
{% unread user%} and works. +10 explain error.
django django-templates
No comments:
Post a Comment