亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

                Django教程-02連接初始化數據庫

                Django教程——01安裝使用

                在上面一篇文章里,介紹了安裝Django的方法,這里說說連接數據庫

                這篇主要介紹踩的坑和解決辦法

                正常連接和初始化數據庫的命令

                python manage.py migrate

                執(zhí)行這個命令的時候,出現了如下報錯

                django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

                經過一段排查,是我配置數據庫的時候,多嵌套了一層default,修改為如下即可

                DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.mysql’, ‘NAME’: ‘dbname’, ‘USER’: ‘root’, ‘PASSWORD’: ‘****’, ‘HOST’: ‘127.0.0.1’, ‘PORT’: ‘3306’, # ‘ENGINE’: ‘django.db.backends.sqlite3’, # ‘NAME’: BASE_DIR / ‘db.sqlite3’, }}

                然后繼續(xù)執(zhí)行migrate時,會報如下錯誤

                django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.Did you install mysqlclient?

                本機環(huán)境是mac電腦,按官方教程操作的時候,發(fā)現安裝mysqlclient的python包會依賴本機安裝mysql或者mysql-client,但在裝mysql和mysql-client的時候,發(fā)現一直報錯。后面發(fā)現解決辦法是,在settings.py文件里,加下如下代碼,即改用pymsql連接即可。

                import pymysqlpymysql.install_as_MySQLdb()

                或者在settings.py同目錄的__init__.py里加如上代碼也可以

                然后再執(zhí)行python manage.py migrate命令會發(fā)現表順利創(chuàng)建。新增表,不影響原有庫的其他表。

                Running migrations: Applying contenttypes.0001_initial… OK Applying auth.0001_initial… OK Applying admin.0001_initial… OK Applying admin.0002_logentry_remove_auto_add… OK Applying admin.0003_logentry_add_action_flag_choices… OK Applying contenttypes.0002_remove_content_type_name… OK Applying auth.0002_alter_permission_name_max_length… OK Applying auth.0003_alter_user_email_max_length… OK Applying auth.0004_alter_user_username_opts… OK Applying auth.0005_alter_user_last_login_null… OK Applying auth.0006_require_contenttypes_0002… OK Applying auth.0007_alter_validators_add_error_messages… OK Applying auth.0008_alter_user_username_max_length… OK

                鄭重聲明:本文內容及圖片均整理自互聯網,不代表本站立場,版權歸原作者所有,如有侵權請聯系管理員(admin#wlmqw.com)刪除。
                用戶投稿
                上一篇 2022年6月21日 06:46
                下一篇 2022年6月21日 06:47

                相關推薦

                聯系我們

                聯系郵箱:admin#wlmqw.com
                工作時間:周一至周五,10:30-18:30,節(jié)假日休息