views:

964

answers:

1

So I am just starting out on learning Django, and I'm attempting to complete one of the sample applications from the book. I'm getting stuck now on creating DRY URL's. More specifically, I cannot get my context processor to work. I create my context processor as so:

from django.conf import settings
#from mysite.settings import ROOT_URL

def root_url_processor(request):
  return {'ROOT_URL': settings.ROOT_URL}

and I placed this file in my app, specifically, mysite/photogallery/context_processors.py . My settings.py file in the root of my project contains:

TEMPLATE_CONTEXT_PROCESSORS = ('mysite.context_processors',)

When I try to go to the ROOT_URL that I've also specified in my settings.py, I receive this error:

TypeError at /gallery/

'module' object is not callable

/gallery/ is the ROOT_URL of this particular application. I realize that perhpas this could mean a naming conflict, but I cannot find one. Furthermore, when I comment out the TEMPLATE_CONTEXT_PROCESSORS definition from settings.py, the application actually does load, however my thumbnail images do not appear (probably because my templates do not know about ROOT_URL, right?). Anyone have any ideas as to what the problem could be?

EDIT: Here's some information about my settings.py in case it is of use:

ROOT_URLCONF = 'mysite.urls'

ROOT_URL = '/gallery/'
LOGIN_URL = ROOT_URL + 'login/'
MEDIA_URL = ROOT_URL + 'media/'
ADMIN_MEDIA_PREFIX = MEDIA_URL + 'admin/'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

TEMPLATE_CONTEXT_PROCESSORS = ('mysite.photogallery.context_processors',)

EDIT2: I'm going to add some information about my url files. Essentially I have a root urls.py, a real_urls.py which is also located at the root, and a urls.py that exists in the application. Basically, root/urls.py hides ROOT_URL from real_urls.py, which then includes my app's urls.py.

root/urls.py:

from django.conf.urls.defaults import *
#from mysite.settings import ROOT_URL
from django.conf import settings

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    (r'^blog/', include('mysite.blog.urls')),
                       url(r'^%s' % settings.ROOT_URL[1:], include('mysite.real_urls')),
    )

root/real_urls.py:

from django.conf.urls.defaults import *
from django.contrib import admin

urlpatterns = patterns('', url(r'^admin/(.*)', admin.site.root),
                       url(r'^', include('mysite.photogallery.urls')),
                       )

root/photogallery/urls.py (note that this one probably is not causing any of the problems, but I'm adding it here in case anyone wants to see it.):

from django.conf.urls.defaults import *
from mysite.photogallery.models import Item, Photo

urlpatterns = patterns('django.views.generic', url(r'^$', 'simple.direct_to_template', kwargs={'template': 'index.html', 'extra_context': {'item_list': lambda: Item.objects.all()}
                                                                                               },
                                                   name='index'), url(r'^items/$', 'list_detail.object_list', kwargs={'queryset': Item.objects.all(), 'template_name': 'items_list.html', 'allow_empty': True },
                                                                      name='item_list'), url(r'^items/(?P<object_id>\d+)/$', 'list_detail.object_detail', kwargs={'queryset': Item.objects.all(), 'template_name': 'items_detail.html' }, name='item_detail' ), url(r'^photos/(?P<object_id>\d+)/$', 'list_detail.object_detail', kwargs={'queryset': Photo.objects.all(), 'template_name': 'photos_detail.html' }, name='photo_detail'),)
+4  A: 

TEMPLATE_CONTEXT_PROCESSORS should contain a list of callable objects, not modules. List the actual functions that will transform the template contexts. Link to docs.

TokenMacGuy
Thank you very much! The book was rather confusing about this; I appended root_url_processor to the end of my TEMPLATE_CONTEXT_PROCESSORS and it worked.
AlbertoPL
Don't rely too much on books. Great for learning, but if you get stuck, check online documentation. If you're still stuck, you might even find the answer in the source code. obviously SO is still a great option.
TokenMacGuy
@TokenMacGuy: Read the source -- excellent advice.
S.Lott