Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, template context processors

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

like image 416
Hellnar Avatar asked Feb 11 '10 18:02

Hellnar


People also ask

What is template context processors in Django?

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context.

Does Django use Jinja2?

Jinja is officially supported by Django, and even before that there were third-party packages that allowed you to use it. The only real compatibility issue is that you can't use Django's custom template tags in a Jinja template.

What does {{ NAME }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What is DTL in Django?

Django Template Language (DTL) is the primary way to generate output from a Django application. You can include DTL tags inside any HTML webpage. The basic DTL tag you can include in an HTML webpage is: 1. {% Tag %}


2 Answers

You need to add the default values of TEMPLATE_CONTEXT_PROCESSORS. However, instead of hard-coding those values, which will be tied to a specific version of Django, you can append your context processor to the default values by the following:

from django.conf import global_settings TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (     "myapp.processor.foos", ) 

Make sure to include the trailing comma in the tuple, so that Python recognizes it as a tuple.

like image 115
Greg Glockner Avatar answered Sep 20 '22 23:09

Greg Glockner


When you specify this:

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

In your settings file, you are overriding the Django's default context processors. In order to extend the list, you need to include the default 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.

like image 32
TM. Avatar answered Sep 23 '22 23:09

TM.