SORU
29 Ocak 2010, Cuma


İstek django özel erişim şablonu etiketler

Myapp_extras.py benim kod:

from django import template

register = template.Library()

@register.inclusion_tag('new/userinfo.html')
def address():
    address = request.session['address']
    return {'address':address}

'': . settings.py içinde

TEMPLATE_CONTEXT_PROCESSORS =(
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    'django.core.context_processors.request'
)

ama bir hata var:

TemplateSyntaxError at /items/

Caught an exception while rendering: global name 'request' is not defined

Original Traceback (most recent call last):
  File "C:\Python25\lib\site-packages\django\template\debug.py", line 71, in render_node
    result = node.render(context)
  File "C:\Python25\lib\site-packages\django\template\__init__.py", line 915, in render
    dict = func(*args)
  File "C:\p4\projects\myproject\..\myproject\invoice\templatetags\myapp_extras.py", line 9, in address
    address = request.session['address']
NameError: global name 'request' is not defined

Bu başvurulan http://stackoverflow.com/questions/335231/in-django-is-it-possible-to-access-the-current-user-session-from-within-a-custom.

CEVAP
29 Ocak 2010, Cuma


request bu kapsamda bir değişkendir. İçerikten önce almak zorunda kalacak. Pass takes_context to the decorator and add context to the tag arguments.

Bu gibi:

@register.inclusion_tag('new/userinfo.html', takes_context=True)
def address(context):
    request = context['request']
    address = request.session['address']
    return {'address':address}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • habpsu

    habpsu

    25 Temmuz 2007
  • khloe brooks

    khloe brooks

    25 Temmuz 2011
  • Pocketnow

    Pocketnow

    14 EKİM 2007