如何解决Django'ModuleNotFoundError'没有名为'___ main ___。models'的模块

时间:2019-11-11 18:52:12

标签: python django

我只想将module导入我的views.py文件中。

我尝试了不同的SO解决方案,例如检查应用程序中可用的___init.py___以及应用程序在installed_apps中的settings中。一切似乎都已正确设置。

仍然出现错误。发生什么事了?

在这里,我要导入模块:

from django.shortcuts import render
from .models.py import ratesEUR

import json
import requests

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()


def render_Quotes_app(request, template="Quotes_app/templates/Quotes_app/Quotes_app.html"):
    return render(request, template)

这是发生的错误:

Traceback (most recent call last):
  File "C:\Users\Jonas\Desktop\dashex\Quotes_app\views.py", line 2, in <module>
    from .models.py import ratesEUR
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
[Finished in 0.373s]

我的设置:

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 = []


# Application definition

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

非常感谢您。

1 个答案:

答案 0 :(得分:0)

from models import ratesEUR有效。由于文件位于同一目录中,因此不需要进一步的路径声明。