Django中运行某个py文件报错

from django.contrib.auth.hashers import make_password

password = make_password('123456')

print(password)

报错django.core.exceptions.ImproperlyConfigured: Requested setting PASSWORD_HASHERS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
因为我们用的是django中的函数,所以在运行前要给他一个Django的环境,并且运行这个Django程序

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE','daily_fresh.settings')
# 其中daily_fresh 是你的项目名
django.setup()

问题解决!

你可能感兴趣的:(Django中运行某个py文件报错)