我希望只使用forms.py将字段显示为纯文本,并在此处找到一个代码段
from django.utils.safestring import mark_safe
class PlainTextWidget(forms.Widget):
def render(self, _name, value, _attrs=None):
return mark_safe(value) if value is not None else '-'
然后在我的forms.py中我一直使用它
class DeleteSiteForm(forms.ModelForm):
class Meta:
model = SiteData
fields = ['location']
widgets = {
'location' : forms.CharField(widget=PlainTextWidget),
}
当我加载页面时,我收到错误:
Traceback:
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/itapp/itapp/sites/views.py" in delete_site
875. from sites.forms import DeleteSiteForm
File "/itapp/itapp/sites/forms.py" in <module>
136. class DeleteSiteForm(forms.ModelForm):
File "/usr/local/lib/python3.6/site-packages/django/forms/models.py" in __new__
266. apply_limit_choices_to=False,
File "/usr/local/lib/python3.6/site-packages/django/forms/models.py" in fields_for_model
182. formfield = f.formfield(**kwargs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in formfield
1110. return super(CharField, self).formfield(**defaults)
File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in formfield
891. return form_class(**defaults)
File "/usr/local/lib/python3.6/site-packages/django/forms/fields.py" in __init__
228. super(CharField, self).__init__(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/forms/fields.py" in __init__
109. extra_attrs = self.widget_attrs(widget)
File "/usr/local/lib/python3.6/site-packages/django/forms/fields.py" in widget_attrs
246. if self.max_length is not None and not widget.is_hidden:
Exception Type: AttributeError at /sites/delete_site/99
Exception Value: 'CharField' object has no attribute 'is_hidden'
什么可能导致此错误,因为我没有看到该属性设置在任何地方?
编辑:
我认为修复小部件现在可能已经破坏了脆弱的形式? 也值得一提我的实际目标。我希望有一个删除页面的提示,就像我们在用户删除记录时进入django admin一样,除了我目前正在做的事情之外,还有一个简单的方法吗?
由于
模板错误:
In template /usr/local/lib/python3.6/site-packages/crispy_forms/templates/bootstrap3/field.html, error at line 28
render() got an unexpected keyword argument 'name' 18 :
19 : {% if field|is_checkboxselectmultiple %}
20 : {% include 'bootstrap3/layout/checkboxselectmultiple.html' %}
21 : {% endif %}
22 :
23 : {% if field|is_radioselect %}
24 : {% include 'bootstrap3/layout/radioselect.html' %}
25 : {% endif %}
26 :
27 : {% if not field|is_checkboxselectmultiple and not field|is_radioselect %}
28 : {% if field|is_checkbox and form_show_labels %}
29 : <label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
30 : {% crispy_field field %}
31 : {{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
32 : </label>
33 : {% include 'bootstrap3/layout/help_text_and_errors.html' %}
34 : {% else %}
35 : <div class="controls {{ field_class }}">
36 : {% crispy_field field %}
37 : {% include 'bootstrap3/layout/help_text_and_errors.html' %}
38 : </div>
Traceback:
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/itapp/itapp/sites/views.py" in delete_site
891. 'delete_site_form': form,
File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
207. return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
107. return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
177. return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
107. return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
72. result = block.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/crispy_forms/templatetags/crispy_forms_tags.py" in render
199. c = self.get_render(context).flatten()
File "/usr/local/lib/python3.6/site-packages/crispy_forms/templatetags/crispy_forms_tags.py" in get_render
118. actual_form.form_html = helper.render_layout(actual_form, node_context, template_pack=self.template_pack)
File "/usr/local/lib/python3.6/site-packages/crispy_forms/helper.py" in render_layout
308. template_pack=template_pack
File "/usr/local/lib/python3.6/site-packages/crispy_forms/layout.py" in render
140. return self.get_rendered_fields(form, form_style, context, template_pack, **kwargs)
File "/usr/local/lib/python3.6/site-packages/crispy_forms/layout.py" in get_rendered_fields
104. for field in self.fields
File "/usr/local/lib/python3.6/site-packages/crispy_forms/layout.py" in <genexpr>
104. for field in self.fields
File "/usr/local/lib/python3.6/site-packages/crispy_forms/utils.py" in render_field
148. html = template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
207. return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
107. return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
322. return nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
322. return nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
322. return nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
993. bits.append(force_text(bit))
File "/usr/local/lib/python3.6/site-packages/django/utils/encoding.py" in force_text
76. s = six.text_type(s)
File "/usr/local/lib/python3.6/site-packages/django/utils/html.py" in <lambda>
385. klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py" in __str__
41. return self.as_widget()
File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py" in as_widget
127. **kwargs
Exception Type: TypeError at /sites/delete_site/99
Exception Value: render() got an unexpected keyword argument 'name'
答案 0 :(得分:2)
小部件字典应该只包含小部件,而不是字段。
widgets = {
'location' : PlainTextWidget,
}