Django上下文处理器故障 [英] Django Context Processor Trouble

查看:61
本文介绍了Django上下文处理器故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我刚开始学习Django,并且尝试完成本书中的示例应用程序之一。我现在无法创建DRY URL。更具体地说,我无法使上下文处理器正常工作。我这样创建上下文处理器:

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}

,然后将此文件放置在我的应用程序中,特别是mysite / photogallery / context_processors.py中。项目根目录中的settings.py文件包含:

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',)

当我尝试转到我也在settings.py中指定的ROOT_URL时,出现此错误:

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

/ gallery /

TypeError at /gallery/

'module'对象不可调用

'module' object is not callable

/ gallery /是此特定应用程序的ROOT_URL。我意识到perhpas可能意味着命名冲突,但是我找不到。此外,当我从settings.py中注释掉TEMPLATE_CONTEXT_PROCESSORS定义时,实际上会加载该应用程序,但是不会显示我的缩略图(可能是因为我的模板不了解ROOT_URL,对吗?)。任何人都对问题可能有什么想法?

/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?

编辑:以下是有关我的settings.py的一些信息(如果使用):

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 :我将添加一些有关我的url文件的信息。本质上,我有一个根urls.py,一个real_urls.py(也位于根目录)和一个存在于应用程序中的urls.py。基本上,root / urls.py在real_urls.py中隐藏了ROOT_URL,然后其中包含我应用程序的urls.py。

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:

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:

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 / phot ogallery / urls.py(请注意,这个问题可能不会引起任何问题,但是我在这里添加了它,以防有人想看到它。):

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'),)


推荐答案

TEMPLATE_CONTEXT_PROCESSORS 应该包含可调用对象列表,而不是模块。列出将转换模板上下文的实际功能。 链接到文档

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.

这篇关于Django上下文处理器故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆