I have the following error when i run the views.py
file. Note that it worked properly before I imported the ratesEUR
model:
views.py (as per @sayse's reply):
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={})
according error:
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',
]
What I have already tried:
- Insert and save
export DJANGO_SETTINGS_MODULE=dashex.settings
to the bottom of the code invenv/bin/activate
but I don't have such directory in myvirtualenv folder at all.
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
- running
export DJANGO_SETTINGS_MODULE=dashex.settings
. This resulted in a syntax error within my shell.
Link: ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured - Scraper
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
- Inserted
DJANGO_SETTINGS_MODULE = DASHEX.settings
into my settings.py file according to the official Django documentation, but that didn't have any effect.
Link: https://docs.djangoproject.com/en/1.10/topics/settings/#designating-the-settings
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
So where and how can I define the environment variable DJANGO_SETTINGS_MODULE
as suggested in the error itself to -probably- resolve this issue?
In advance thank you very much for your assistance!