python 参数“field”应该包含一个有效的Django BoundField?

lf3rwulv  于 2022-12-17  发布在  Python
关注(0)|答案(2)|浏览(145)

我正在使用django-bootstrap 3在模板上渲染我的表单,我一直在努力寻找是什么导致错误Parameter "field" should contain a valid Django BoundField当我试图加载页面上的表单。我已经附上了我的代码和错误如下。有人能请我指出我做错了什么吗?
forms.py

class OrderForm(forms.Form):
    first_name = forms.CharField(max_length=50)
    last_name = forms.CharField(max_length=50)
    email = forms. EmailField(max_length=50)
    institution_name = forms.CharField(max_length=150)
    phone = forms.IntegerField()
    address = forms.CharField(max_length=100)
    city = forms.CharField(max_length=50)
    item = forms.CharField(max_length=100)
    serial_number = forms.CharField(max_length=50)
    problem = forms.CharField(widget=forms.Textarea(attrs—Crows':10,'cols':18,'style':'resize:none',  [placeholder':'Please define your problem here'l),label='Problem description')
    [placeholder':'Please define your problem here'l),label='Problem description')

views.py
x一个a一个b一个x一个m一个n一个x
``
模板x1c 0d1x
浏览器错误

控制台日志

23c0lvtd

23c0lvtd1#

这可能是因为某些字段可能丢失。请查看this
您可以执行类似下面的操作来查看哪些字段可用:

<form role="form" method="post">
     {% csrf_token %}
     {% bootstrap_form order_form %}
     {% buttons submit='OK' reset="Cancel" %}{% endbuttons %}
 </form>

然后试着找出为什么会有缺失的字段。

dnph8jn4

dnph8jn42#

我找到了适合我的方法。
稍微调试一下,就会发现这是在www.example.com中抛出renderers.py:

class FieldRenderer(BaseRenderer):
"""Default field renderer."""

# These widgets will not be wrapped in a form-control class
WIDGETS_NO_FORM_CONTROL = (CheckboxInput, RadioSelect, CheckboxSelectMultiple, FileInput)

def __init__(self, field, *args, **kwargs):
    if not isinstance(field, BoundField):
        raise BootstrapError('Parameter "field" should contain a valid Django BoundField.')
    self.field = field
    super().__init__(*args, **kwargs) ...

这只是一个没有帮助的消息的例子,做我所做的,只在你的开发/测试环境中,你将永远不会在这个神秘的消息上浪费时间。
警告:以下内容将使您的保修无效!:)
在www.example.com的渲染链中向上走几步library.py(例如~/virtualenvs/your_env/lib/python3.8/site/packages/django/template/library.py),然后找到SimpleNode。

class SimpleNode(TagHelperNode):

def __init__(self, func, takes_context, args, kwargs, target_var):
    super().__init__(func, takes_context, args, kwargs)
    self.target_var = target_var

def render(self, context):
    resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
    output = self.func(*resolved_args, **resolved_kwargs)
    if self.target_var is not None:
        context[self.target_var] = output
        return ''
    if context.autoescape:
        output = conditional_escape(output)
    return output

如下所示更改它,用try/except将“output = self.func...” Package 起来:

class SimpleNode(TagHelperNode):

def __init__(self, func, takes_context, args, kwargs, target_var):
    super().__init__(func, takes_context, args, kwargs)
    self.target_var = target_var

def render(self, context):
    resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
    #
    from bootstrap3.exceptions import BootstrapError
    try:
        output = self.func(*resolved_args, **resolved_kwargs)
    except BootstrapError as b:
        raise BootstrapError('Parameter "' + self.args[0].token + '" should contain a valid Django BoundField.')
    #
    if self.target_var is not None:
        context[self.target_var] = output
        return ''
    if context.autoescape:
        output = conditional_escape(output)
    return output

现在您将得到一个包含实际问题字段的异常报告。干杯。

相关问题