如何解决Django中的“请求的设置INSTALLED_APPS,但未配置设置。”错误?

时间:2019-11-11 19:52:14

标签: python django

运行views.py文件时出现以下错误。请注意,它在之前正常运行,我导入了ratesEUR模型:

views.py(根据@sayse的回复):

from django.shortcuts import render
from models import ratesEUR
import json
import requests

def my_view(request):

response = requests.get("http://data.fixer.io/api/latest?access_key=XXX&base=EUR")

    rates_EUR = json.loads(response.content.decode('utf-8'))
    timestamp = rates_EUR['timestamp']
    base = rates_EUR['base']
    date = rates_EUR['date']
    rates = rates_EUR['rates']
    id = 1

    rates_new = ratesEUR(id=id, timestamp=timestamp, base=base, date=date, rates=rates)
    rates_new.save()

    return response(data={})

根据错误:

Traceback (most recent call last):
  File "C:\Users\Jonas\Desktop\dashex\Quotes_app\views.py", line 2, in <module>
    from models import ratesEUR
  File "C:\Users\Jonas\Desktop\dashex\Quotes_app\models.py", line 4, in <module>
    class ratesEUR(models.Model):
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\conf\__init__.py", line 64, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[Finished in 0.41s]

models.py:

from django.db import models


class ratesEUR(models.Model):
    timestamp = models.CharField(max_length=10)
    base = models.CharField(max_length=3)
    date = models.DateField(auto_now=False, auto_now_add=False)
    rates = models.CharField(max_length=8)

    def __str__(self):
        return self.base

settings.py:

import os.path
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/


STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join('static'), )


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["127.0.0.1", "locahost"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Quotes_app',
    'Wiki_app',
    'rest_framework',
]

项目目录:
enter image description here

我已经尝试过的方法:

  • export DJANGO_SETTINGS_MODULE=dashex.settings插入并保存到venv/bin/activate中的代码底部,但是我的myvirtualenv文件夹中根本没有这样的目录。

链接:ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

<> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < > <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < / p>

  • 运行export DJANGO_SETTINGS_MODULE=dashex.settings。这导致我的Shell中出现语法错误。

链接:ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured - Scraper

<> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < > <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < / p>

  • 根据官方Django文档将DJANGO_SETTINGS_MODULE = DASHEX.settings插入我的settings.py文件中,但这没有任何效果。

链接:https://docs.djangoproject.com/en/1.10/topics/settings/#designating-the-settings

<> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < > <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> < / p>

那么我该在哪里以及如何根据错误本身的建议来定义the environment variable DJANGO_SETTINGS_MODULE,以“可能”解决该问题?

在此先非常感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您的视图不是视图,它只是在从views.py首次导入时将运行的代码,您需要使其成为视图

def my_view(request):

    response = requests.get("http://data.fixer.io/api/latest?access_key=XXX&base=EUR")

    rates_EUR = json.loads(response.content.decode('utf-8'))
    timestamp = rates_EUR['timestamp']
    base = rates_EUR['base']
    date = rates_EUR['date']
    rates = rates_EUR['rates']
    id = 1

    rates_new = ratesEUR(id=id, timestamp=timestamp, base=base, date=date, rates=rates)
    rates_new.save()

    return JsonResponse(data={})

答案 1 :(得分:1)

您真正的问题是,您尝试直接运行views.py脚本,而不是django的工作原理,您可以通过设置的url访问视图。

您的两个选择,

  • 都可以像通常一样使用url调用视图

  • 使用Django shell

python manage.py shell

from .views import my_view
my_view(None)

答案 2 :(得分:0)

第一个建议是使用from .models而不是from models,第二个建议是缩进响应,使它位于视图cos中,因为问题一经出现,您将在语法问题上看到语法错误2通过了。

settings.py是什么样的?您是否将应用程序添加到已安装的应用程序变量中?