使用Sqlite3和Python 2.7将数据库添加到Django项目 [英] Adding a database to the Django Project using Sqlite3 with Python 2.7
问题描述
我试图使用Sqlite3和Python 2.7将数据库添加到Django项目。
这是我的setting.py的样子:
DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',#Add'postgresql_psycopg2','mysql','sqlite3'or'oracle'。
'NAME':'dev.db',#或使用sqlite3时数据库文件的路径。
'USER':'',#不与sqlite3一起使用。
'PASSWORD':'',#不与sqlite3一起使用。
'HOST':'',#为localhost设置为空字符串。不与sqlite3一起使用。
'PORT':'',#默认为空字符串。不与sqlite3一起使用。
}
}
它允许我创建数据库,创建超级用户:
您刚刚安装了Django的验证系统,这意味着您没有定义任何超级用户。您要立即创建一个吗? (是/否):
当我键入yes时,它会给我这个错误msg。 >
错误msg:
回溯(最近最后一次呼叫):
文件manage.py,第10行,在< module>
execute_from_command_line(sys.argv)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__.py,第443行execute_from_command_line
utility.execute()
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__.py,第382行执行
self.fetch_command(subcommand).run_from_argv(self.argv)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/base .py,第196行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages /django/core/management/base.py,第232行,在执行
中output = self.handle(* args,** options)
File/ Users / IMAC / work3 / env / lib /python2.7/site-packages/django/core/management/base.py,第371行,在句柄
中return self.handle_noargs(** options)
File/ Users / IMAC / work3 /env/lib/python2.7/site-packages/django/core/management/commands/syncdb.py,第110行,在handle_noargs
emit_post_sync_signal(created_models,verbosity,interactive,db)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/sql.py,第189行,在emit_post_sync_signal
interactive = interactive,db = db)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/dispatch/dispatcher.py,第172行,发送
response = receiver(signal = self,sender = sender,** named)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py,第73行,在create_superuser
call_command(createsuperuser,interactive = True,database = db)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__ .py,行150,在call_command中
return klass.execute(* args,** defaults)
文件/Users/IMAC/work3/env/lib/python2.7/site-packages/ django / core / management / base.py,行232,在执行
output = self.handle(* args,**选项)
文件/ Users / IMAC / work3 / env / python2.7 / site-packages / django / contrib / auth / management / commands / createsuperuser.py,第70行,在句柄
default_username = get_default_username()
文件/ Users / IMAC / work3 / en / lib / python2.7 / site-packages / django / contrib / auth / management / __init__.py,行105在get_default_username
中default_username = get_system_username()
文件/ Users / IMAC /在get_system_username
中的第85行,返回getpass.getuser()。decode(locale.getdefaultlocale()),在这个例子中是work3 / env / lib / python2.7/site-packages/django/contrib/auth/management/__init__.py [1])$ b $ b文件/Users/IMAC/work3/env/lib/python2.7/locale.py,第496行,在getdefaultlocale中
return _parse_localename(localename)
文件 /Users/IMAC/work3/env/lib/python2.7/locale.py,第428行,位于_parse_localename
raise ValueError,'未知区域设置:%s'%localename
ValueError:未知区域设置: UTF-8
如何解决上述错误?为什么不允许我添加超级用户?我该怎么办?
在执行python manage.py syncdb
之前键入这个问题,export LC_CTYPE = en_US.UTF-8
export LC_ALL = en_US.UTF-8
这将解决错误.. Btw我使用mac os x python它在/ usr / bin / python。
$ b将它添加到./.bash_profile文件中,以便自动调用它。
I am trying to add a database to the Django Project using Sqlite3 and Python 2.7.
This is how my setting.py looks like:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'dev.db', # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } }
It allows me to create the database and it asks me to create the superuser:
You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no):
When i type yes, it gives me this error msg.
Error msg:
Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line utility.execute() File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv self.execute(*args, **options.__dict__) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute output = self.handle(*args, **options) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle return self.handle_noargs(**options) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 110, in handle_noargs emit_post_sync_signal(created_models, verbosity, interactive, db) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in emit_post_sync_signal interactive=interactive, db=db) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send response = receiver(signal=self, sender=sender, **named) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 73, in create_superuser call_command("createsuperuser", interactive=True, database=db) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command return klass.execute(*args, **defaults) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute output = self.handle(*args, **options) File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 70, in handle default_username = get_default_username() File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username default_username = get_system_username() File "/Users/IMAC/work3/env/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username return getpass.getuser().decode(locale.getdefaultlocale()[1]) File "/Users/IMAC/work3/env/lib/python2.7/locale.py", line 496, in getdefaultlocale return _parse_localename(localename) File "/Users/IMAC/work3/env/lib/python2.7/locale.py", line 428, in _parse_localename raise ValueError, 'unknown locale: %s' % localename ValueError: unknown locale: UTF-8
How do i solve the error above? Why doesn't it allow me to add a superuser? what should i do? Could this problem because I am running Python 2.7?
解决方案Type this before you execute the python manage.py syncdb
export LC_CTYPE=en_US.UTF-8 export LC_ALL=en_US.UTF-8
This will solve the error.. Btw i was using mac os x python which was in /usr/bin/python.
Add it to the ./.bash_profile file so that it calls it automatically..
这篇关于使用Sqlite3和Python 2.7将数据库添加到Django项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!