views:

44

answers:

3

I want to use an app to create a menu that is easy to edit with the admin interface. Something like this:

class Menu_item
        name = models.CharField()
        item_url = models.URLField()

My template looks something like this:

{% extends base.html %}

div ID="nav"

   {{ foo.navbar.? }}

/div

div ID="Content"

   {% block content %}{% endblock %}

/div

I want div#nav to contain a ul based upon the above model but just can't figure out how to accomplish this. It seems like an object_list generic view would be great but, the URL accesses the view for the model that populates div#content. Does anyone have any suggestions? Is there a way to access a generic view without a URL?

Thank you.

A: 

First, in you view, get data from db:

def index(request):
    navbar = Menu_item.objects.all()
    return render_to_response( 'you_template.html', 
        { 'navbar': navbar }, context_instance = RequestContext (request ) )

And in template:

<div id="nav">
    <ul>
    {% for i in navbar %}
        <li><a href="{{ i.item_url }}">{{ i.name }}</a></li>
    {% endfor  %}
    </ul>
</div>
dikamilo
I am receiving an error with this view function from the shell (and no items are rendered). Does it require a context processor? Also, the main content block is rendered by a generic view.
Nickfly
What kind of error ? I think you don't have "from django.shortcuts import render_to_response".
dikamilo
I will post the error below
Nickfly
A: 

Here is the error:

    Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/projects/mysite/menu/views.py", line 8, in index
    { 'navbar': navbar }, context_instance = RequestContext (request ) )
  File "/usr/lib/python2.6/dist-packages/django/template/context.py", line 149, in __init__
    self.update(processor(request))
  File "/usr/lib/python2.6/dist-packages/django/core/context_processors.py", line 53, in debug
    if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
AttributeError: 'int' object has no attribute 'META'
Nickfly
A: 
Nickfly