DJango TypeError - JSFiles 对象不可逆

DJango TypeError - JSFiles object is not reversible

我试图在我的 DJango 2.0.2 版中包含 this datepicket widget,但它引发了错误

Request Method: GET
Request URL: http://127.0.0.1:8000/xuser/1/edit
Django Version: 2.0.2
Exception Type: TypeError
Exception Value: 'JSFiles' object is not reversible
Exception Location: D:\DevTools\Python\lib\site-packages\django\forms\widgets.py in merge, line 114
Python Executable: D:\DevTools\Python\python.exe
Python Version: 3.6.3

我已经在以下线程中寻找类似的错误

但这些只是关于 urlpatterns 拼写错误 ,甚至没有透露 python 中可逆的实际含义。 google search 关于可逆对象不会产生比上述线程更多的东西。所以,我的问题是,

Traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/xuser/1/edit

Django Version: 2.0.2
Python Version: 3.6.3
Installed Applications:
['bootstrap3',
 'bootstrap_datepicker',
 'core.apps.CoreConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
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 D:\Python\dj_abstractbaseuser2\core\templates\core\edit.html, error at line 8
   'JSFiles' object is not reversible
   1 : {# Load the tag library #}
   2 : {% load bootstrap3 %}
   3 : 
   4 : {# Load CSS and JavaScript #}
   5 : {% bootstrap_css %}
   6 : {% bootstrap_javascript %}
   7 : {% block extrahead %}
   8 :  {{ form.media }} 
   9 : {% endblock %}
   10 : 
   11 : {# Display django.contrib.messages as Bootstrap alerts #}
   12 : {% bootstrap_messages %}
   13 : <form method="post">
   14 :     {% csrf_token %}
   15 :     {% bootstrap_field form.first_name %}
   16 :     {% bootstrap_field form.last_name %}
   17 :     {% bootstrap_field form.date_of_birth %}
   18 :     {% buttons %}


Traceback:

File "D:\DevTools\Python\lib\site-packages\django\forms\forms.py" in __getitem__
  157.             field = self.fields[name]

During handling of the above exception ('media'), another exception occurred:

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in _resolve_lookup
  835.                     current = current[bit]

File "D:\DevTools\Python\lib\site-packages\django\forms\forms.py" in __getitem__
  163.                     ', '.join(sorted(f for f in self.fields)),

During handling of the above exception ("Key 'media' not found in 'UserEditForm'. Choices are: date_of_birth, first_name, last_name."), another exception occurred:

File "D:\DevTools\Python\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "D:\DevTools\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "D:\DevTools\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "D:\DevTools\Python\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "D:\DevTools\Python\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "D:\DevTools\Python\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\loader_tags.py" in render
  58.                 result = self.nodelist.render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in render
  993.             output = self.filter_expression.resolve(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in resolve
  676.                 obj = self.var.resolve(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in resolve
  802.             value = self._resolve_lookup(context)

File "D:\DevTools\Python\lib\site-packages\django\template\base.py" in _resolve_lookup
  843.                         current = getattr(current, bit)

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in _media
  148.             base = sup_cls.media

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in _media
  148.             base = sup_cls.media

File "D:\DevTools\Python\lib\site-packages\django\forms\forms.py" in media
  460.             media = media + field.widget.media

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in _media
  148.             base = sup_cls.media

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in _media
  163.                 return m + Media(definition)

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in __add__
  135.         combined._js = self.merge(self._js, other._js)

File "D:\DevTools\Python\lib\site-packages\django\forms\widgets.py" in merge
  114.         for path in reversed(list_2):

Exception Type: TypeError at /xuser/1/edit
Exception Value: 'JSFiles' object is not reversible

错误是由this issue submitted to the widget's Github page, and has not yet been solved. So I switched to django-bootstrap-datepicker-plus引起的,它在我的DJango 2.0.2版本中运行得很好。但问题仍然需要回答。

What is a reversible object? Why we get this Error?

错误是由 python built-in 函数 reversed 引发的,该函数将序列作为唯一参数并反转序列。

reversed() Parameters
seq - sequence that should be reversed.

  • Could be an object that supports sequence protocol (__len__() and __getitem__() methods) as tuple, string, list or range
  • Could be an object that has implemented __reversed__()

因此所有序列(例如列表、元组)都是可逆的。但是要使对象可逆,我们需要实现一个名为 __reversed__ 的方法,或者同时实现 __len__()__getitem__() 方法。如果我们将对象传递给不满足上述任何条件的 reversed() 函数,它将引发异常 TypeError - object is not reversible.

DJango 2.0 将此 reversed() 函数用于任何小部件添加的资产。由于上述小部件中使用的 JSFiles 对象未实现可逆标准,因此引发了错误。