python error AttributeError:'str'对象没有属性'setdefault' [英] python error AttributeError: 'str' object has no attribute 'setdefault'
问题描述
我正在尝试使用此命令运行django项目.
I'm trying to run the django project using this command.
python manage.py runserver 8080
但是每次我尝试运行时,都会遇到这样的错误.
But everytime I'm trying to run I faced the such a error.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/utils.py", line 209, in __getitem__
self.ensure_defaults(alias)
File "/Users/admin/.virtualenvs/myprojectname/lib/python2.7/site-packages/django/db/utils.py", line 181, in ensure_defaults
conn.setdefault('ATOMIC_REQUESTS', False)
AttributeError: 'str' object has no attribute 'setdefault'
我使用virtualenvwrapper尝试了python2(python2.7.11)和python3(python3.5.1).我认为这不是项目来源的错误.但是在环境配置中遗漏了一些东西.但我不知道是什么问题.请帮我修复它.
I tried python2(python2.7.11) and python3(python3.5.1) with virtualenvwrapper. I think it's not the bug of the project source. but something missed in environment configuration. But I can't figure out what the problem is. Please help me fix it.
谢谢.
推荐答案
DATABASES字典中的每个元素本身必须是字典.您已将默认"项覆盖为一个字符串.
Each element in the DATABASES dict must itself be a dict. You have overwritten the 'default' entry to just be a string.
由于您使用的是sqlite3和默认的数据库名称,因此您应该只恢复到设置的原始版本:
Since you are using sqlite3 and the default database name, you should just revert to the original version of the setting:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
这篇关于python error AttributeError:'str'对象没有属性'setdefault'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!