Issue #2 new
Nick b
created an issue

Hi,

I have implemented the package as suggested by the documentation. i.e. - pip install - add to settings - {% load menu_item %} {% menu_item "/foo/" "Foo" %}

However, this results in a keyerror using django 1.4.2 and direct_to_template

Environment:

Request Method: GET
Request URL: http://mysite.com/

Django Version: 1.4.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'app',
 'django_contact_form',
 'menus')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template templates/base.html, error at line 22
   /foo/


   18 : {% load menu_item %}


   19 : <nav>


   20 : <ul>


   21 : 


   22 :  {% menu_item "/home/" "Home" %} 


   23 : {% menu_item "/about/" "About" %}


   24 : 


   25 : </ul>


   26 : </nav>


   27 : {% endblock %}



Traceback:
File "/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/myproject/views.py" in index
  5.     return direct_to_template(request, 'index.html')
File "/lib/python2.7/django/views/generic/simple.py" in direct_to_template
  28.     return HttpResponse(t.render(c), content_type=mimetype)
File "/lib/python2.7/django/template/base.py" in render
  140.             return self._render(context)
File "/lib/python2.7/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/lib/python2.7/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/lib/python2.7/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/lib/python2.7/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/lib/python2.7/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/lib/python2.7/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/lib/python2.7/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/lib/python2.7/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/lib/python2.7/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/lib/python2.7/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/lib/python2.7/menus/templatetags/menu_item.py" in render
  129.             url = resolve_path(url, context)
File "/lib/python2.7/menus/templatetags/menu_item.py" in resolve_path
  87.         current = current[obj]
File "/lib/python2.7/django/template/context.py" in __getitem__
  54.         raise KeyError(key)

Exception Type: KeyError at /
Exception Value: u'/foo/'

Comments (5)

  1. Emilio Ramirez

    Instead of:

    {% menu_item "/home/" "Home" %} 
    

    try:

    {% menu_item "url:your_app.views.your_view" "Home" %} 
    # Or
    {% menu_item "url:your_view_name" "Home" %}  # your_view_name means url(...., name=your_view_name)
    
  2. Emilio Ramirez

    @Matthew Henderson no, the first don't work :(

    Template

    {% li_menu_item "/cartera/home/clientes" "Tomadores" %}
    

    Traceback

    Traceback:
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      111.                         response = callback(request, *callback_args, **callback_kwargs)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
      20.                 return view_func(request, *args, **kwargs)
    File "/home/emilio/Laburo/infoxel/brokerdigital/elportalunico/../elportalunico/cartera/views.py" in default
      50.     return home(request)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
      20.                 return view_func(request, *args, **kwargs)
    File "/home/emilio/Laburo/infoxel/brokerdigital/elportalunico/../elportalunico/cartera/views.py" in home
      63.     return render_to_response('h5_base_header_home.html', locals(), context_instance=RequestContext(request))
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
      20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
      176.         return t.render(context_instance)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in render
      140.             return self._render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in _render
      134.         return self.nodelist.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in render
      823.                 bit = self.render_node(node, context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
      74.             return node.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
      123.         return compiled_parent._render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in _render
      134.         return self.nodelist.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in render
      823.                 bit = self.render_node(node, context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
      74.             return node.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
      123.         return compiled_parent._render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in _render
      134.         return self.nodelist.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in render
      823.                 bit = self.render_node(node, context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
      74.             return node.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
      62.             result = block.nodelist.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/base.py" in render
      823.                 bit = self.render_node(node, context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
      74.             return node.render(context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/menus/templatetags/menu_item.py" in render
      154.             url = resolve_path(url, context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/menus/templatetags/menu_item.py" in resolve_path
      99.     return reduce(_get, path.split('.'), context)
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/menus/templatetags/menu_item.py" in _get
      94.         return x[y]
    File "/home/emilio/.virtualenvs/infoxel/local/lib/python2.7/site-packages/django/template/context.py" in __getitem__
      54.         raise KeyError(key)
    
    Exception Type: KeyError at /
    Exception Value: u'/cartera/home/clientes'
    
  3. jonathandoughty

    I found that with Django 1.5.1 and the recommended "url:your ..." forms I was getting "TemplateSyntaxError ... menu_item tag requires 'request' in context"

    I found that by adding:

    from django.conf import global_settings
    TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
        'django.core.context_processors.request',
    )
    

    to my settings.py that django-menus' menu_items now work as advertised; thank you very much.

  4. Log in to comment