开发模板上下文处理器时出错

时间:2015-01-25 16:19:01

标签: django django-templates

我正在开发我的第一个模板上下文处理器。

它产生了一个错误:Module "bookings.context_processors" does not define a "context_processors" attribute/class

我已经遵循了一些例子,但这对我来说并没有多大意义。有人能告诉我为什么会产生这个错误吗?

代码非常简单:

context_processors.py

from bookings.models import Booking, BookingItem

def injectBookingObject(request):
    try:
        booking = request.session['bookingID']
        bookingOptions = BookingItem.objects.all().filter(bookingID = booking)
        return {
            'bookingContents' : bookingOptions,
        }
    except:
        return {}

设置:

TEMPLATE_CONTEXT_PROCESSORS = (
    *** usual ones *****
    'bookings.context_processors'
)

views.py

def activity_detail(request, slug):
    if request.method == 'GET':
        selectedactivity = get_object_or_404(Activity, urlKey=slug)
        activityMedia = ActivityMedia.objects.all().filter(
            Activity = selectedactivity, MediaStatus = 1,
            MediaActiveDate__lte=datetime.datetime.now(), 
            MediaInactiveDate__gte=datetime.datetime.now()
        ).exclude(MediaType = 'T').order_by('-MediaType', 'MediaSortOrder')

        activityOptions = ActivityOption.objects.all().filter(
            Activity = selectedactivity,
            ActivityOptionActiveDate__lte=datetime.datetime.now(), 
            ActivityOptionInactiveDate__gte=datetime.datetime.now()
        ) | ActivityOption.objects.all().filter(
                Activity = selectedactivity, 
                ActivityOptionActiveDate__lte=datetime.datetime.now(), 
                ActivityOptionInactiveDate=None
            )
        equipment = Equipment.objects.all().filter(
            activity = selectedactivity
        )
        instructor = User.objects.get(id=int(selectedactivity.Instructor.id))
        context = {
            'activity': selectedactivity,
            'activityMedia' : activityMedia,
            'activityOptions' : activityOptions,
            'activityEquipment' : equipment,
            'instructor' : instructor,
        }
        return render(
            request,
            'activities/activity.html',
            context
        )

1 个答案:

答案 0 :(得分:4)

你需要

TEMPLATE_CONTEXT_PROCESSORS = (
*** usual ones *****
    'bookings.context_processors.injectBookingObject'
)