1

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',
]

Project dir:
enter image description here

What I have already tried:

  • Insert and save export DJANGO_SETTINGS_MODULE=dashex.settings to the bottom of the code in venv/bin/activate but I don't have such directory in myvirtualenv folder at all.

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

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

  • 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!

JSRB
  • 2,492
  • 1
  • 17
  • 48
  • All of that code is currently outside of a view – Sayse Nov 11 '19 at 19:54
  • Can't follow your input :-/ – JSRB Nov 11 '19 at 19:57
  • Aside from your imports and last 2 line of your views.py, its all outside of a view, what is it supposed to be doing? – Sayse Nov 11 '19 at 19:58
  • The idea is to call data from a third party API and populate the model `ratesEUR` with it. (models.py added to initial post) – JSRB Nov 11 '19 at 20:00
  • As initial data? You should make a management command – Sayse Nov 11 '19 at 20:01
  • I want the view to make that API call every 120 seconds to decrease API calls and render the frontend using the data populated to the SQLite database (in order to avoid one API call per website visitor). That's basically my idea. – JSRB Nov 11 '19 at 20:03
  • Btw this is the stuff behind: https://stackoverflow.com/questions/58790507/how-to-use-axios-vue-js-to-call-data-from-sqlite-django-backend – JSRB Nov 11 '19 at 20:06
  • How are you calling your view? – Sayse Nov 11 '19 at 20:23
  • I use the `script` package in atom editor and use the option `run script` to execute the views.py file. – JSRB Nov 11 '19 at 20:25

3 Answers3

1

Your view, isn't a view, its just code that will be ran when first importing from views.py, you need to make it a view

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={})
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Thank you for the input! I updated the `views.py` but still having the same error when i run it. Additionally the server debugs `No module named models`. Something seems to be weird still. – JSRB Nov 11 '19 at 20:17
1

Your real issue is that you're trying to run the views.py script directly, that isn't how django works, views are there to be accessed via a url that you have set.

Your two options,

  • either call the view like normal with a url

  • use the django shell

python manage.py shell

from .views import my_view
my_view(None)
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Got it! Thank you very much. One last question: Say I like to fetch the data every 120 seconds from the external API, a view might be a bad idea then because I would need to visit the url every 120 seconds? What would be a proper way to fetch the third party API every 120 seconds using Django/python and populate the data to a model? Thank you! – JSRB Nov 11 '19 at 20:42
  • @Phanti - That wouldn't be a bad idea at all, thats exactly what you'd need to do, and its exactly what you're doing anyway with the third party api's endpoint – Sayse Nov 11 '19 at 20:45
  • But how to make the view execute every 120 seconds if nobody visits the related url? (This question probably is pretty foolish :-p). – JSRB Nov 11 '19 at 20:47
  • 1
    Research into task runners - celery etc. – Sayse Nov 11 '19 at 20:48
0

First suggestion is to use from .models not from models, and second would be to indent the response so it lives in the view cos as it stands you'll get a syntax error after the problem on line 2 is passed.

What does settings.py look like? Did you add the app to the installed apps variable?

ItIsEntropy
  • 306
  • 3
  • 13
  • Using `from .models` I end up with this error: `Traceback (most recent call last): File "C:\Users\Jonas\Desktop\dashex\Quotes_app\views.py", line 1, in from .models import ratesEUR ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package [Finished in 0.09s]` Added `settings.py` to initial post. – JSRB Nov 11 '19 at 20:31