请求对象在Django 1.11中的模板标记中不可用

时间:2017-07-12 09:57:54

标签: django

我一直在使用Django 1.9,现在我将代码升级到1.11 我有这个模板标签在1.9

中运行良好
{% url "test:index" as home_url %}
<li class="{% active request home_url %}"><a href="{% url 'test:index' %}">Calendar</a></li>

我的base.html

'str' object has no attribute 'path'

但现在传递的是一个空字符串而不是请求对象,所以我收到了这个错误

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'test', 'templates')],
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.template.context_processors.debug',
            'django.template.context_processors.i18n',
            'django.template.context_processors.media',
            'django.template.context_processors.static',
            'django.contrib.messages.context_processors.messages',
        ],
        'loaders': [
            'admin_tools.template_loaders.Loader',
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
        ],
    },
},

这是我正在使用的模板设置

Traceback:

File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/spare/local/projects/towerportal/apps/tower_common/decorators.py" in _fn
  42.             return t.render(context,request)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  207.                     return self._render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/spare/local/venv/lib/python2.7/site-packages/django/template/library.py" in render
  203.         output = self.func(*resolved_args, **resolved_kwargs)

File "/spare/local/projects/test/apps/portal/templatetags/active_util.py" in active
  12.     if re.search(pattern, request.path):

Exception Type: AttributeError at /caldb/
Exception Value: 'str' object has no attribute 'path'

这是追溯

def render_view(template, with_print_view=False):
    def _decorator(view):
        @wraps(view)
        def _fn(request, *args, **kwargs):
            context = view(request, *args, **kwargs)

            # if view returns a response, return it
            if isinstance(context, HttpResponse):
                return context

            if with_print_view:
                if request.GET.get('print_view') == 'y':
                    context['original_template'] = template
                    return render(request, 'print_view.html', context)
                else:
                    context['show_print_button'] = True

            return render_to_response(template, context, RequestContext(request))

        return _fn

    return _decorator

这是渲染视图的装饰器。

$connection = Yii::$app->getDb();

$command = $connection->createCommand("UPDATE table_name SET account_code = CONCAT(account_type, "-", account_type2, "-", account_type3, "-", account_type4) WHERE SOME_condition");

$result = $command->queryAll();

2 个答案:

答案 0 :(得分:2)

您应该在模板标签装饰器上使用takes_context标志。

@register.simple_tag(takes_context=True)
def active(context, pattern):
     import re

     pattern = "^" + pattern + "$"
     request = context['request']
     if re.search(pattern, request.path):
         return 'active'
     return ''

Here the docs reference

答案 1 :(得分:2)

停止使用render_to_response,它已经过时了。在Django 1.10中,context_instance已被删除,因此您的代码相当于:

render_to_response(template, context=None, content_type=RequestContext(request))

您应该使用render代替。将装饰器中的返回行更改为:

return render(request, template, context)

当您在Django 1.9中运行代码时,您应该有关于该问题的弃用警告。