Django:无法导入模块

时间:2016-03-30 14:26:29

标签: python django

我正在尝试将views.py中的模块导入为

from django.shortcuts import render

# Create your views here.
from viewcreator import Builder
import json
def index(request):

    a,b=Builder.buildChartJSON()
    print(json.dumps(a))  
    print(json.dumps(b))
    return render(request, 'hdfsStats/hdfscharts.html',
                  {'sourcepoints': a, 'sizepoints': b})

以及我的项目设置如何

enter image description here

为什么我不能在我的视图中导入模块?我不想在models.py中创建这些类。这些类只是为了运行一些计算并返回两个json对象,然后将其提供给我的网页

这是我的settings.py

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.9.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '9mxuqginh(vhh*2eu6j58kbq+%+7ql4_pn3k#yf+n96uv0rymq'

# 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',
    'hdfsStats.apps.HdfsstatsConfig'
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

我缺少任何配置吗?

堆栈跟踪

File "XXX:\mysite\hdfsStats\urls.py", l
ine 3, in <module>
    from . import views
  File "XXX:\mysite\hdfsStats\views.py",
line 5, in <module>
    from viewcreator import Builder
    ImportError: No module named 'viewcreator'

更新

目前的结构

enter image description here

和我的views.py

来自django.shortcuts导入渲染

# Create your views here.
from .src.viewcreator import Builder
import json
def index(request):

    a,b=Builder.buildChartJSON()
    print(json.dumps(a))  
    print(json.dumps(b))
    return render(request, 'hdfsStats/hdfscharts.html',
                  {'sourcepoints': a, 'sizepoints': b})

但现在我

from propreader import ReadProp
ImportError: No module named 'propreader'
基本上,我有四个包

viewcreator
propreader
esconnector
ping

这些包中的类根据这两个文件夹中的属性文件执行一些计算

props
resources

我把它放在与src文件夹

相同的级别

由于这些是一次性计算,我不想为这些创建模型。在这种情况下,我配置Django项目的正确方法是什么?我需要Django项目来托管一个显示我的计算结果的网页。

1 个答案:

答案 0 :(得分:10)

在您的INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'HdfsstatsConfig',
]
你的views.py中的

from .viewcreator import Builder

UPDATE,DJANGO IMPORTS

在django中有三种导入模块的方法

<强> 1。绝对导入: 从当前应用程序外部导入模块 例 来自myapp.views导入HomeView enter image description here

<强> 2。明确导入: 从当前应用程序内部导入模块 enter image description here

第3。相对导入: 与显式导入相同,但不推荐

from models import MyModel

enter image description here