gpt4 book ai didi

python - Django 错误 : TypeError: 'NoneType' object has no attribute '__getitem__'

转载 作者:太空狗 更新时间:2023-10-30 01:41:34 32 4
gpt4 key购买 nike

使用 Django(对它来说是新手,对 Python 来说也是新手)做一个非常基本的联系人数据库。

尝试通过管理页面将记录添加到贡献者表,其模型代码为:

class Contributor(models.Model):

first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
organisation = models.ForeignKey('Organisation')
mode = models.CharField(max_length=10, blank=True)
email = models.CharField(max_length=30, blank=True)
landline = models.CharField(max_length=10, blank=True)

def __unicode__(self):
print('%s %s' % (self.first_name, self.last_name))

我得到的错误信息是:

    Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/ramapp/contributor/add/

Django Version: 1.5.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'south',
'ramapp')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/db/transaction.py" in inner
223. return func(*args, **kwargs)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
1009. self.log_addition(request, new_object)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_addition
530. action_flag = ADDITION
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/models.py" in log_action
18. e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)

Exception Type: TypeError at /admin/ramapp/contributor/add/
Exception Value: 'NoneType' object has no attribute '__getitem__'

感谢任何帮助。挣扎!谢谢。

马特

最佳答案

您在 __unicode__ 方法中打印,而不是返回字符串。

关于python - Django 错误 : TypeError: 'NoneType' object has no attribute '__getitem__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19816994/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com