【问题标题】:DatabaseError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestringsDatabaseError: 除非您使用可以解释 8 位字节串的 text_factory,否则不得使用 8 位字节串
【发布时间】:2011-08-02 15:03:24
【问题描述】:

我正在尝试为 django 应用程序设置数据库。

因此,当我尝试创建数据库时,除了一件事外,一切正常。最后,出现以下消息:

您刚刚安装了 Django 的身份验证系统,这意味着您没有定义任何超级用户。 您想现在创建一个吗? (是/否):

如果我输入“yes”并按回车键,则会出现以下错误:

D:\xampp\htdocs\Django>cd myapps

D:\xampp\htdocs\Django\myapps>python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 191,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 351,
 in handle
    return self.handle_noargs(**options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 109, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 190,
in emit_post_sync_signal
    interactive=interactive, db=db)
  File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 172,
in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Python27\lib\site-packages\django\contrib\auth\management\__init__.py
", line 70, in create_superuser
    call_command("createsuperuser", interactive=True)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
166, in call_command
    return klass.execute(*args, **defaults)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\contrib\auth\management\commands\cr
eatesuperuser.py", line 72, in handle
    User.objects.get(username=default_username)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 132, in
 get
    return self.get_query_set().get(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 344, in g
et
    num = len(clone)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 82, in __
len__
    self._result_cache = list(self.iterator())
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 273, in i
terator
    for row in compiler.results_iter():
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 68
0, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 73
5, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 34, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
234, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: You must not use 8-bit bytestrings unless you use
 a text_factory that can interpret 8-bit bytestrings (like text_factory = str).
It is highly recommended that you instead just switch your application to Unicod
e strings.

D:\xampp\htdocs\Django\myapps>

我相信如果我选择“是”,它应该提示我输入用户名和密码,但它不会提示我任何提示并给我这个错误。

有什么建议吗?

【问题讨论】:

    标签: python django sqlite


    【解决方案1】:

    看起来这是由于bug in Django related to system usernames that contain non-8-bit characters

    作为一种解决方法,您可以在要求创建超级用户时说“不”并使用createsuperuser 命令创建一个具有不同名称的新超级用户:

    $ python manage.py syncdb
    Creating table auth_permission
    Creating table auth_group_permissions
    Creating table auth_group
    Creating table auth_user_user_permissions
    Creating table auth_user_groups
    Creating table auth_user
    Creating table auth_message
    Creating table django_content_type
    Creating table django_session
    Creating table django_site
    
    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): no
    Installing index for auth.Permission model
    Installing index for auth.Group_permissions model
    Installing index for auth.User_user_permissions model
    Installing index for auth.User_groups model
    Installing index for auth.Message model
    No fixtures found.
    $ python manage.py createsuperuser --username yourname
    E-mail address: email@example.com
    Password: 
    Password (again): 
    Superuser created successfully.
    

    【讨论】:

    • 我发现了问题所在。问题是我的 Windows 登录名 Şenol 中的土耳其字符。所以我所做的是,在 Windows 中创建了一个没有任何土耳其字符的新用户,然后使用该用户帐户登录。而且效果很好。
    • 超级用户在 django 中扮演什么角色?我点击了否,我现在是否需要创建一个超级用户才能使用 django?
    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2013-04-26
    • 2011-06-17
    相关资源
    最近更新 更多