' NoneType'对象没有属性' __ getitem __'

时间:2014-06-24 11:18:32

标签: python django amazon-s3 sorl-thumbnail

在尝试访问管理员面板中的用户个人资料时,我开始遇到此问题。

TypeError at /admin/users/userprofile/2/
'NoneType' object has no attribute '__getitem__'

在我的应用程序中集成Django Storages以使用Amazon S3存储服务之后,它才开始发生。我无法弄清楚这里有什么冲突。回溯似乎表明问题在于我用于我的用户图像字段的sorl thubmnails。

这是我的settings.py

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

AWS_ACCESS_KEY_ID = 'my-access-key'
AWS_SECRET_ACCESS_KEY = 'my-secret-key'
AWS_STORAGE_BUCKET_NAME = 'my-bucket-name'

STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

这是我的用户模型

from sorl.thumbnail import ImageField

class UserProfile(AbstractBaseUser):
    email = models.EmailField(max_length=255, unique=True)
    username = models.CharField(max_length=100, unique=True)
    name = models.CharField(max_length=255)
    location = models.CharField(max_length=255, blank=True, null=True)
    website = models.CharField(max_length=255, blank=True, null=True)
    image = ImageField(upload_to='media', blank=True, null=True)
    points = models.PositiveIntegerField(default=0)
    added_on = models.DateTimeField(auto_now_add=True)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

以下是问题的追溯

Environment:


Request Method: GET
Request URL: http://www.localhost/admin/users/userprofile/2/

Django Version: 1.6.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'south',
 'rest_framework',
 'django_extensions',
 'debug_toolbar',
 'django_jinja',
 'django_statsd',
 'django_user_agents',
 'alphafilter',
 'redis_status',
 'subdomains',
 'pipeline',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.twitter',
 'allauth.socialaccount.providers.google',
 'sorl.thumbnail',
 'storages',
 'brands',
 'cachier',
 'categories',
 'crawler',
 'deals',
 'features',
 'facets',
 'products',
 'stores',
 'users',
 'util')
Installed Middleware:
('buyingiq.middleware.TimerMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'subdomains.middleware.SubdomainURLRoutingMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django_user_agents.middleware.UserAgentMiddleware',
 'buyingiq.middleware.VisitorMiddleware')


Template error:
In template /home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
   'NoneType' object has no attribute '__getitem__'
   9 :             {% for field in line %}


   10 :                 <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>


   11 :                     {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}


   12 :                     {% if field.is_checkbox %}


   13 :                         {{ field.field }}{{ field.label_tag }}


   14 :                     {% else %}


   15 :                         {{ field.label_tag }}


   16 :                         {% if field.is_readonly %}


   17 :                             <p>{{ field.contents|linebreaksbr }}</p>


   18 :                         {% else %}


   19 :                              {{ field.field }} 


   20 :                         {% endif %}


   21 :                     {% endif %}


   22 :                     {% if field.field.help_text %}


   23 :                         <p class="help">{{ field.field.help_text|safe }}</p>


   24 :                     {% endif %}


   25 :                 </div>


   26 :             {% endfor %}


   27 :         </div>


   28 :     {% endfor %}


   29 : </fieldset>


Traceback:
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  196.                         nodelist.append(node.render(context))
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  155.         return self.render_template(self.template, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render_template
  137.         output = template.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  196.                         nodelist.append(node.render(context))
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  196.                         nodelist.append(node.render(context))
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  305.                 return nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  305.                 return nodelist.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render
  91.             output = force_text(output)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  100.                 s = s.__unicode__()
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in __str__
  425.         return self.as_widget()
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in as_widget
  475.         return widget.render(name, self.value(), attrs=attrs)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/sorl/thumbnail/admin/current.py" in render
  34.                          ) % (mini.width, value.url, mini.url, output)
File "/home/jaskaran/coding/buyingiq/buyingiq/venv/local/lib/python2.7/site-packages/sorl/thumbnail/images.py" in width
  51.         return self.size[0]

Exception Type: TypeError at /admin/users/userprofile/2/
Exception Value: 'NoneType' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:1)

错误是由图像属性的缺失图像文件引起的。检查用户的UserProfile.image的文件路径(ID = 2)。

可能是您将代码和数据库从一个位置复制到另一个位置,但您没有复制关联的图像文件。