1. 程式人生 > >Django 執行test.py模組時報錯“settings are not configured

Django 執行test.py模組時報錯“settings are not configured

Django 執行test.py模組時報錯“settings are not configured”

錯誤資訊

django.core.exceptions.ImproperlyConfigured:

Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call

settings.configure() before accessing settings.

解決方案

在test.py內引入下面程式碼

import os,django

os.environ['DJANGO_SETTINGS_MODULE'] = 'lu_blog.settings'
django.setup()

重新執行,問題解決。