display a json through a GET request in django permission denied - python

I'd like to display the contents of a JSON file through a GET request.
In views.py I use a GET request to open data2.json. The file is in the same directory as views.py.
But I get the following error:
File "C:\Users\Kaik\Documents\djangoPractice\shopping_cart\api_app\views.py", line 134, in get
with open(pdfPath2,"r") as file:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Kaik\\Documents\\djangoPractice\\shopping_cart\\api_app'
[12/Jun/2022 11:14:04] "GET /study/data2.json HTTP/1.1" 500 90058
Here is the GET in views:
from django.views import View
from django.http import JsonResponse
import json
from shopping_cart import settings
from .models import CartItem
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from . import studyduck_all
from . import iniStudyduck
from iniStudyduck import initiateDuck
import os
def get(self, request, pdfname):
path = os.getcwd()
pdfPath = os.path.join(path, 'api_app')
pdfPath2 = os.path.join(settings.MEDIA_ROOT, 'textfile.txt')
jsonData = ''
with open(pdfPath2,"r") as file:
jsonData = json.load(file)
return JsonResponse(jsonData)
Here is my SETTINGS:
"""
Django settings for shopping_cart project.
Generated by 'django-admin startproject' using Django 2.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os # new
from pathlib import Path
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("HERE IS THE BASE DIR ", BASE_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'HIDDEN'
# 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',
'api_app',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'shopping_cart.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'shopping_cart.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/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/2.2/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',
},
]
STATICFILES_DIRS = [
BASE_DIR + "/static",
BASE_DIR + '/api_app/PDFs',
]
print("STATICFILES_DIRS" , STATICFILES_DIRS)
# Internationalization
# https://docs.djangoproject.com/en/2.2/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/2.2/howto/static-files/
STATIC_URL = '/static/'
# Path where media is stored # TEMP
MEDIA_ROOT = os.path.join(BASE_DIR, 'api_app/PDFs')
and here is urls.py
from django.urls import path
from .views import ShoppingCart, ShoppingCartUpdate, StudyDuck
urlpatterns = [
path('cart-items/', ShoppingCart.as_view()),
path('update-item/<int:item_id>', ShoppingCartUpdate.as_view()),
path('study/<str:pdfname>', StudyDuck.as_view()),
]
I've tried a number of things including changing location of data2.json to be reflected in settings static files, tried to change permissions (I'm on a windows but not sure how to do this on Windows)....
Any help would be appreciated.

Related

Django ModuleNotFoundError: No module named 'EmailIngestionDemo.EmailIngestionDemo'

Im trying to write a django web that will retrieve data from postgresql and display on it. But whenever I run my application it show me:
from EmailIngestionDemo.EmailIngestionDemo.models import EmailData
ModuleNotFoundError: No module named 'EmailIngestionDemo.EmailIngestionDemo'
On my views.py I import my EmailData from EmailIngestionDemo.EmailIngestionDemo.models:
Views.py
from django.shortcuts import render
from EmailIngestionDemo.EmailIngestionDemo.models import EmailData
def showdata(request):
results = EmailData.obj.all()
return render(request, 'index.html',{"data":results})
This is my path:
settings:
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxx'
# 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',
'EmailIngestionDemo'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'EmailIngestionDemo.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 = 'EmailIngestionDemo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME':"EmailData",
'USER':"postgres",
'PASSWORD':"xxx",
'HOST':"localhost",
'PORT':'5432'
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Try from .models import EmailData
get rid of the __init__.py adjacent to your manage.py - the level with manage.py should not be a Python package
use EmailIngestionDemo.models instead

ModuleNotFoundError: No module named 'django_backend.todo'

Just started a fresh Django Rest framework project and I'm getting stuck on importing a view (TodoView) in urls.py
This is a completely fresh project, all I did was add the model, view and now attempting to add the url.
File "C:\Users\Simon\Documents\GitHub\tradingjournal\django_backend\django_backend\urls.py", line 20, in <module>
from django_backend.todo.views import TodoView
ModuleNotFoundError: No module named 'django_backend.todo'
urls.py
from django.contrib import admin
from django.urls import path
from rest_framework import routers
from django_backend.todo.views import TodoView
router = routers.DefaultRouter()
router.register(r'todos', TodoView, 'todo')
urlpatterns = [
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
# Create your views here.
from rest_framework import viewsets
from django_backend.todo.models import Todo
from django_backend.todo.serializers import TodoSerializer
class TodoView(viewsets.ModelViewSet):
serializer_class = TodoSerializer
queryset = Todo.objects.all()
settings.py
"""
Django settings for django_backend project.
Generated by 'django-admin startproject' using Django 3.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/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/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'n3^zfbj#c&i-9v^8q(%iox!kuy#gy2liy-1b+)q21-g&nwezf('
# 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',
'rest_framework',
'corsheaders',
'todo',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
ROOT_URLCONF = 'django_backend.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'django_backend.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/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/3.0/howto/static-files/
STATIC_URL = '/static/'
I tried changing django_backend.todo.views to todo.views but as you can see in the screenshot it's not recognized:
In a Django project the directory name of the project really doesn't matter in the code so instead of
from django_backend.todo.views import TodoView
you need to be writing:
from todo.views import TodoView
Next you see pycharm complaining that this is not a valid import while in fact it actually is (can be seen by actually running the project). Why does this happen? It is because it considers your projects root directory (i.e. there is likely a directory above django_backend) as the sources root and hence is unable to resolve your imports. To solve this you just need to set it as the Sources root by following these steps:
Right click on the project directory i.e. django_backend (on the project structure on the left side)
Select the option "Mark Directory As"
Click "Sources root"

Django 3, Python 3.8 Can't Find My Templates

I'm running Python 3.8 and Django 3.0.5 on Windows 10. I'm using JetBrains PyCharm 2(Professional Edition) to create and deploy my Django apps.
Here's the code in my views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "firstapp\homes.html")
Views.py
Here's the line in my settings.py:
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/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y********
# 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',
'firstapp',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Hello.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 = 'Hello.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/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/3.0/howto/static-files/
STATIC_URL = '/static/'
Here's the line in my urls.py
from django.contrib import admin
from django.urls import path, re_path
from firstapp import views
urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
]
I cannot understand why slash does not work in views.py. And here's Error after I run server. Why slash didn't find a path (url)?
ERROR
return render(request, "firstapp/homes.html")
The way above is the correct way to denote file paths in Python.

Django: ImproperlyConfigured The SECRET_KEY setting must not be empty when executing gunicorn

I'm trying to deploy a django application on AWS. I've read all the existing posts regarding the same issue and include the
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango_with_django.settings")
in manage.py and wsgi.py but still it does not work.
My project structure is the following one:
--chimpy
-botApp
-settings
-base.py
-production.py
-wsgi.py
-manage.py
These are my files:
-base.py:
"""
Django settings for botApp project.
Generated by 'django-admin startproject' using Django 2.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/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/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'whatever'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
LOCALE_PATHS = [
os.path.join(BASE_DIR, '../../locale'),
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
'portfolios',
'django_extensions',
'rest_framework',
'corsheaders',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
ROOT_URLCONF = 'botApp.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',
],
'libraries':{
'proper_paginate': 'portfolios.proper_paginate',
'url_replace': 'portfolios.url_replace',
}
},
},
]
WSGI_APPLICATION = 'botApp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db',
'USER': 'db_user',
'PASSWORD': 'whatever',
'HOST': 'localhost',
'PORT': '',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/howto/static-files/
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "../../static"), '/Users/ibotics/suribit/static', ]
STATIC_ROOT = "/Users/ibotics/suribit/static_cdn"
TEMPLATE_DIRS = ('/templates/',)
LOGIN_URL = '/login'
# media config missing
# static cdn missing?
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
'localhost:8080','localhost:8000',
)
CORS_ORIGIN_REGEX_WHITELIST = (
'localhost:8080','localhost:8000',
)
-production.py:
from .base import *
DEBUG = False
ALLOWED_HOSTS = ['*']
-wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "botApp.settings.production")
application = get_wsgi_application()
-manage.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "botApp.settings.production")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
As you can see all files point to the settings/production.py file, which imports everything from base.py and I have the secret key in there, but still receive the same error Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty when I try to do:
gunicorn botApp.wsgi:application --bind 0.0.0.0:8001
Many thanks.

Django : Cannot import modules

I am trying to import a module in my views.py as
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})
and here is how my project setup looks like
why cant i import the modules in my view? I do not want to create these classes in the models.py. These classes are just meant to run some calculations and return two json objects, which i then feed to my webpage
here is my 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/'
am i missing any configuration?
Stacktrace
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'
Update
current structure
and my views.py
from django.shortcuts import render
# 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})
but now i get
from propreader import ReadProp
ImportError: No module named 'propreader'
basically, i have four packages
viewcreator
propreader
esconnector
ping
the classes in these packages perform some calculations based on the properties files in the these two folders
props
resources
which i have put at the same level as src folder
Since these are one time calculations, i dont want to create models for these. What is the proper way for me to configure my Django project in this scenario? I need the Django project to host a webpage that will display the results of my calculations.
In your INSTALLED_APPS
INSTALLED_APPS = [
...,
'HdfsstatsConfig',
]
in your views.py
from .viewcreator import Builder
UPDATE, DJANGO IMPORTS
There are 3 ways to imports module in django
1. Absolute import:
Import a module from outside your current application
Example
from myapp.views import HomeView
2. Explicit import:
Import a module from inside you current application
3. Relative import:
Same as explicit import but not recommended
from models import MyModel

Categories