使用django-widget-tweaks时的RecursionError

时间:2017-09-25 11:18:20

标签: python django python-3.x

我已在我的django应用上尝试了一项非常基本的负载测试,其中包括以下内容:

for _ in {1..50}; do for x in {1..50}; do curl http://example.com &> /dev/null; done & done

在服务器开始返回http 500之后,原因是RecursionError

Serving on http://unix:/tmp/waitress/socket
WARNING:django.template:Exception raised while rendering {% include %} for template 'django/forms/widgets/email.html'. Empty string rendered instead.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 216, in render
    return template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 166, in render
    values = self.sequence.resolve(context, True)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 708, in resolve
    obj = self.var.resolve(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 849, in resolve
    value = self._resolve_lookup(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 882, in _resolve_lookup
    current = current[bit]
  File "/usr/local/lib/python3.6/site-packages/django/template/context.py", line 84, in __getitem__
    for d in reversed(self.dicts):
RecursionError: maximum recursion depth exceeded while calling a Python object
WARNING:django.template:Exception raised while rendering {% include %} for template 'django/forms/widgets/text.html'. Empty string rendered instead.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 216, in render
    return template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 166, in render
    values = self.sequence.resolve(context, True)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 708, in resolve
    obj = self.var.resolve(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 849, in resolve
    value = self._resolve_lookup(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 882, in _resolve_lookup
    current = current[bit]
  File "/usr/local/lib/python3.6/site-packages/django/template/context.py", line 84, in __getitem__
    for d in reversed(self.dicts):
RecursionError: maximum recursion depth exceeded while calling a Python object
WARNING:django.template:Exception raised while rendering {% include %} for template 'django/forms/widgets/email.html'. Empty string rendered instead.
<...>

经过一段时间(和请求)之后,这看起来如下:

RecursionError: maximum recursion depth exceeded in comparison
ERROR:django.request:Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/django/portal/views.py", line 37, in get
    return render(request, self.template_name, ctx)
  File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 993, in render
    bits.append(force_text(bit))
  File "/usr/local/lib/python3.6/site-packages/django/utils/encoding.py", line 76, in force_text
    s = six.text_type(s)
  File "/usr/local/lib/python3.6/site-packages/django/utils/html.py", line 385, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 41, in __str__
    return self.as_widget()
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  [Previous line repeated 902 more times]
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 101, in as_widget
    attrs = self.build_widget_attrs(attrs, widget)
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 257, in build_widget_attrs
    if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
  File "/usr/local/lib/python3.6/site-packages/django/forms/widgets.py", line 258, in use_required_attribute
    return not self.is_hidden
RecursionError: maximum recursion depth exceeded while calling a Python object
ERROR:django.request:Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/django/portal/views.py", line 37, in get
    return render(request, self.template_name, ctx)
  File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 993, in render
    bits.append(force_text(bit))
  File "/usr/local/lib/python3.6/site-packages/django/utils/encoding.py", line 76, in force_text
    s = six.text_type(s)
  File "/usr/local/lib/python3.6/site-packages/django/utils/html.py", line 385, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 41, in __str__
    return self.as_widget()
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  [Previous line repeated 904 more times]
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 101, in as_widget
    attrs = self.build_widget_attrs(attrs, widget)
RecursionError: maximum recursion depth exceeded
ERROR:django.request:Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/django/portal/views.py", line 37, in get
    return render(request, self.template_name, ctx)
  File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 993, in render
    bits.append(force_text(bit))
  File "/usr/local/lib/python3.6/site-packages/django/utils/encoding.py", line 76, in force_text
    s = six.text_type(s)
  File "/usr/local/lib/python3.6/site-packages/django/utils/html.py", line 385, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py", line 41, in __str__
    return self.as_widget()
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 28, in as_widget
    html = old_as_widget(widget, attrs, only_initial)
  [Previous line repeated 904 more times]
  File "/usr/local/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 27, in as_widget
    process(widget or self.field.widget, attrs, attribute, value)
RecursionError: maximum recursion depth exceeded
ERROR:django.request:Internal Server Error: /

现在看来罪魁祸首可能是django-widget-tweaks,但是呢?我使用widget-tweaks的方式或多或少是从他们的文档中复制/粘贴。

<div class="form-group{% if usr_form.email.errors %} has-error{% endif %}">
    {{ usr_form.email.errors }}
    {% render_field usr_form.email class+="form-control" placeholder="Email Address" %}
</div>

以下内容摘自requirements.txt

Django==1.11.4
django-widget-tweaks==1.4.1

修改

我已将表格重构为:

class UserModelForm(ModelForm):
    class Meta:
        model = models.User
        fields = ['name', 'email']
        widgets = {
            'name': TextInput(attrs={'class': 'form-control', 'placeholder': 'Full Name'}),
            'email': EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email Address'}),
        }

和HTML:

<div class="form-group{% if usr_form.name.errors %} has-error{% endif %}">
    {{ usr_form.name.errors }}
    {{ usr_form.name }}
</div>
<div class="form-group{% if usr_form.email.errors %} has-error{% endif %}">
    {{ usr_form.email.errors }}
    {{ usr_form.email }}
</div>

Haven没有见过http 500,所以它绝对是django-widget-tweaks bug。

0 个答案:

没有答案