Chris Chris - 5 months ago 20
HTML Question

url function in jinja2 template throws AttributeError in Django

I have a

base.html
in
my_project/jinja2/
and it contains everything except the
<body>
for a site. We then extend
base.html
as one does.

In this example I am extending
base.html
from app
people
in a file at
my_project/people/jinja2/people/people_list.html
using something like this:

{% extends "base.html" %}

{% block content %}
<!-- Some html -->
{% endblock content %}


My
base.html
includes a link to the blog rss feed in the
<head>
like so:

<link rel="alternate" type="application/atom+xml" title="Blog" href="{{ url('blog:articles_feed') }}" />


This works fine most places such as
my_project/jinja2/index.html
and
my_project/jinja2/blog_list.html
but in this 3rd app
people
, I am getting the following error at this same line of template code:

AttributeError: 'str' object has no attribute '__call__'


Since jinja2 has better debugging I can run python in werkzueg and see some potential details of what might be happening:

locals()


which outputs:

{
'_': {...},
'static': < bound method StaticFilesStorage.url of < django.contrib.staticfiles.storage.StaticFilesStorage object at 0x7f423ec3ebe0 >> ,
'joiner': < class 'jinja2.utils.Joiner' > ,
'request': < WSGIRequest: GET '/case-studies/hog?__debugger__=yes&cmd=locals()&frm=139922493301984&s=XfAagGnpxRWFBRRd0Uzk' > ,
'page': None,
'csrf_input': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e8262e8 > ,
'cycler': < class 'jinja2.utils.Cycler' > ,
'dict': < class 'dict' > ,
'absolute_url': < function absolute_url at 0x7f423ece5b70 > ,
'lipsum': < function generate_lorem_ipsum at 0x7f423ebfc8c8 > ,
'view': < leaf.views.LeafTemplateView object at 0x7f423e820198 > ,
'range': < class 'range' > ,
'ngettext': < function ungettext at 0x7f42450547b8 > ,
'gettext': < function ugettext at 0x7f4245054730 > ,
'absolute_root': < function absolute_root at 0x7f423ece8268 > ,
'datetime': < class 'datetime.datetime' > ,
'csrf_token': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e826400 > ,
'url': 'people/all'
}


I'm not sure if this is a correct assumption, but is
url
not being correctly added to the environment as a function, but a string instead? It's in my jinja2.py file, and works elsewhere as expected in jinja2 templates. What gives?

Answer

Selcuk had the right idea. I was using jinja templates, and the kwargs are added in automatically. One of my kwargs was <url> and was over-riding the url extension I had added to the jinja environment. Renaming the kwarg to something else solved the issue.