views:

384

answers:

1

Hello, I have a weird problem, I want to add a global query using context processors. This is how I did it by following:

made a processor.py in my app as such:

from myproject.myapp.models import Foo

def foos(request):
    return {'foos': Foo.objects.all()}

and at the end of my setting.py I have added this:

TEMPLATE_CONTEXT_PROCESSORS = ('myapp.processor.foos',)

Lastly I pass my view as this:

def index_view(request):

    return render_to_response('index.html', {}, context_instance=RequestContext(request))

and at my index.html template:

<select id="select_foo">
{% for foo in foos %}
    <option value="/{{ foo.slug }}">{{ foo.name }}</option>
{% endfor %}
</select>

And lastly my url:

(r'^$', 'myapp.views.index_view'),

My foos display without any problem, however my media_url and other contexts are gone. What can be the issue

+3  A: 

When you specify this:

TEMPLATE_CONTEXT_PROCESSORS = ('myapp.processor.foos',)

In your settings file, you are overriding the default context processors that you had before. You need to include the old ones in your settings:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "myapp.processor.foos",
)

Note, the settings above are the defaults (plus your processor) for django 1.1.

TM
I weirdly do not have TEMPLATE_CONTEXT_PROCESSORS in my settings.py , using the default Django 1.1.1 and media_url was working fine earlier.
Hellnar
That's because if you don't specify it, it uses the the default values specified in djangos settings. That's how all django settings work, they have a default that will be used if you don't have it in your `settings.py`.
TM
Thanks now working! I was getting error so I removed "django.contrib.messages.context_processors.messages",I think this is for the development version of django, not 1.1.1
Hellnar
Yes that's right, it is from the dev version. I updated my answer to show the 1.1 version.
TM