Django Select2Widget 没有属性 "attrs"



我正在尝试在表单中实现django_select2,但是当我尝试呈现页面时,出现以下错误:

type object 'Select2Widget' has no attribute 'attrs'

小部件是这样声明的(它是一个模型形式(:

self.fields["prodotti_var"].widget = Select2Widget

以下是完整的回溯:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/magazzino/bolle_in/5/
Django Version: 2.0.6
Python Version: 3.6.5
Installed Applications:
['django_select2',
'actstream',
'amministrazione',
'clienti',
'fornitori',
'prodotti',
'documenti',
'fatture',
'admin_interface',
'colorfield',
'magazzino',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.humanize',
'widget_tweaks']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template error:
In template /home/matteo/Naturelab/amministrazione/templates/base_site.html, error at line 0
type object 'Select2Widget' has no attribute 'attrs'
1 : {% load static %}
2 : {% load widget_tweaks %}
3 : <!DOCTYPE html>
4 : <!--
5 : Template Name: LuMa CMS
6 : Version: 1.0
7 : Author: LucyT & MatFire1999
8 : Contact: hello@lumacms.com
9 : -->
10 : <html lang="it">

Traceback:
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35.             response = get_response(request)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128.                 response = self.process_exception_by_middleware(e, request)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/matteo/Naturelab/magazzino/views.py" in ddt_in_detail
76.     return render(request, "ddt_in_mod.html", context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/shortcuts.py" in render
36.     content = loader.render_to_string(template_name, context, request, using=using)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62.     return template.render(context, request)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/backends/django.py" in render
61.             return self.template.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render
175.                     return self._render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in _render
167.         return self.nodelist.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render
943.                 bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910.             return self.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/loader_tags.py" in render
155.             return compiled_parent._render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in _render
167.         return self.nodelist.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render
943.                 bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910.             return self.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/loader_tags.py" in render
67.                 result = block.nodelist.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render
943.                 bit = node.render_annotated(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910.             return self.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/defaulttags.py" in render
211.                     nodelist.append(node.render_annotated(context))
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910.             return self.render(context)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in render
197.         return str(bounded_field)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/utils/html.py" in <lambda>
380.     klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/django/forms/boundfield.py" in __str__
36.         return self.as_widget()
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in as_widget
30.         process(widget or self.field.widget, attrs, attribute, value)
File "/home/matteo/virtualenvs/Naturelab/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in process
63.         elif widget.attrs.get(attribute):
Exception Type: AttributeError at /magazzino/bolle_in/5/
Exception Value: type object 'Select2Widget' has no attribute 'attrs'

你们能帮我吗?

通过编写:

self.fields["prodotti_var"].widget = Select2Widget

没有构造小部件,而是将对Select2Widget类的引用传递到self.fields中。

您需要调用构造函数,因此:

self.fields["prodotti_var"].widget = Select2Widget()

相关内容

  • 没有找到相关文章

最新更新