简体   繁体   English

Django上下文处理器麻烦

[英]Django Context Processor Trouble

So I am just starting out on learning Django, and I'm attempting to complete one of the sample applications from the book. 所以我刚刚开始学习Django,我正在尝试完成本书中的一个示例应用程序。 I'm getting stuck now on creating DRY URL's. 我现在因为创建DRY URL而陷入困境。 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 . 我把这个文件放在我的应用程序中,特别是mysite / photogallery / context_processors.py。 My settings.py file in the root of my project contains: 我的项目根目录中的settings.py文件包含:

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: 当我尝试转到我在settings.py中指定的ROOT_URL时,收到此错误:

TypeError at /gallery/ / gallery /的TypeError

'module' object is not callable 'module'对象不可调用

/gallery/ is the ROOT_URL of this particular application. / gallery /是此特定应用程序的ROOT_URL。 I realize that perhpas this could mean a naming conflict, but I cannot find one. 我意识到perhpas这可能意味着命名冲突,但我找不到一个。 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?). 此外,当我从settings.py注释掉TEMPLATE_CONTEXT_PROCESSORS定义时,应用程序实际上会加载,但是我的缩略图图像没有出现(可能是因为我的模板不知道ROOT_URL,对吧?)。 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: 编辑 :以下是有关我的settings.py的一些信息,以防它使用:

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. EDIT2 :我要添加一些关于我的url文件的信息。 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. 基本上我有一个根urls.py,一个也位于根目录的real_urls.py和一个存在于应用程序中的urls.py。 Basically, root/urls.py hides ROOT_URL from real_urls.py, which then includes my app's urls.py. 基本上,root / urls.py会从real_urls.py中隐藏ROOT_URL,然后包含我的应用程序的urls.py.

root/urls.py: 根/ 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: 根/ 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.): root / photogallery / urls.py(请注意,这可能不会导致任何问题,但我在这里添加它以防万一有人想看到它。):

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 should contain a list of callable objects, not modules. TEMPLATE_CONTEXT_PROCESSORS应包含可调用对象的列表,而不是模块。 List the actual functions that will transform the template contexts. 列出将转换模板上下文的实际函数。 Link to docs . 链接到文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM