django wsgi setup with settings subfolder - django

I'm trying differents environment settings for my project, below is my project folder structure:
|-app
|-project
|-__init__.py
|-settings
|-__init__.p
|-base.py
|-dev.py
|-prod.py
|-urls.py
|-wsgi.py
In base.py, how can i setup WSGI_APPLICATION django settings variable to point wsgi file on parent folder ?
File wsgi.py:
if base.STATUS == 'DEV':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.dev')
application = get_wsgi_application()
elif base.STATUS == 'PROD':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.prod')
application = Cling(get_wsgi_application())
else:
raise Exception('Settings App Error !')
File base.py:
...
WSGI_APPLICATION = 'project.wsgi.application'
...
File dev:
from project.settings.base import *
ALLOWED_HOSTS = ['127.0.0.1']
DEBUG = TRUE
File prod.py:
from project.settings.base import *
ALLOWED_HOSTS = ['domain']
DEBUG = FALSE
The error is:
django.core.exceptions.ImproperlyConfigured: WSGI application 'project.wsgi.application' could not be loaded; Error importing module.
Thanks in advance.

Your WSGI file is named wsgy.py, but Django expects wsgi.py. Rename the file to wsgi.py.

You need to have an __init__.py file in your project folder to import your project folder as a module. Just make an empty __init__.py file in the project directory.

SOLVED
The differents configuration environments are ok the problem was with a library dependency:
The requirements.py file was:
django-static
instead of:
dj-static
The strange is the error thrown was:
django.core.exceptions.ImproperlyConfigured
instead of:
ModuleNotFoundError

Related

Issue loading admin.site.urls after changing the settings folder

I re-organized Django,the following way:
config
- settings
- base.py
- local.py
urls.py
wsgi.py
In base.py/local.py:
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
In manage.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
In wsgi.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
I have the following error on runserver:
\django\contrib\admin\sites.py", line 269, in get_urls
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'AccountAdmin' object has no attribute 'urls'
It is related to this line:
path('admin/', admin.site.urls), # Django 2.0 syntax
If I comment that line I get the following error:
django\contrib\admin\sites.py", line 79, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'AccountAdmin' object has no attribute 'model
The app admin is in installed app, I don't know what is creating this issue.
Hmm... Several things happen here. One thing at a time:
Under your settings dir put an __init__.py file with the following contents in it:
from .base import *
try:
from .local import *
LIVE = False
except ImportError:
LIVE = True
if LIVE:
try:
from .production import *
except ImportError:
pass
By putting this inside the __init__.py file, you can reference to your settings file simply with 'config.settings', leaving local or production unreferenced (the __init__.py will handle them).
Now that this is out of way, change both uwsgi.py and manage.py to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
Assuming that you have done all that, it should work (that's how I structure my projects for years and had never any problems). Otherwise, please update your question with project structure and base.py and local.py contents to work it out.

Declare Django settings to use with mod_wsgi/apache

I have multiple settings files in my Django project (base.py, local.py, production.py).
Locally, I'm kicking off the Django dev server via
python manage.py runserver --settings=config.settings.local
How can I tell mod_wsgi to use the same file? I'm testing this on my local apache instance, but will need to figure out the same thing for my production apache instance.
--settings=config.settings.production
Current httpd-vhosts.conf:
WSGIDaemonProcess secureDash python-path=/Users/user/projects/secureDash_project/secureDash python-home=/Users/user/.venvs/securedash_py3.6
WSGIProcessGroup secureDash
WSGIScriptAlias / /Users/user/projects/secureDash_project/config/wsgi.py
Error that I see in the apache log:
ModuleNotFoundError: No module named 'secureDash.settings'
Django 1.11
mod_wsgi-4.5.15
Apache 2.4.18
You do this by setting the DJANGO_SETTINGS_MODULE environment variable. You can either do this in your Apache vhost configuration, or in the wsgi file itself.
I solved this by adding DJANGO_SETTINGS_MODULE to my secrets.json file in each environment.
secrets.json:
{
"FILENAME": "secrets.json",
"SECRET_KEY": "someSuperSecretiveSecret!",
"DJANGO_SETTINGS_MODULE": "config.settings.local"
}
wsgi.py:
import json
from django.core.exceptions import ImproperlyConfigured
from pathlib import Path
...
# Directory Paths
BASE_DIR = Path(__file__).resolve().parent
# JSON-based secrets module
SECRETS_JSON = str(BASE_DIR) + '/secrets.json'
with open(SECRETS_JSON) as f:
secrets = json.loads(f.read())
def get_secret(setting, secrets=secrets):
'''Get the secret variable or return explicit exception'''
try:
return secrets[setting]
except KeyError:
error_msg = 'Set the {0} environment variable'.format(setting)
raise ImproperlyConfigured(error_msg)
DJANGO_SETTINGS_MODULE = get_secret('DJANGO_SETTINGS_MODULE')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DJANGO_SETTINGS_MODULE)

Scrapy project can't find django.core.management

I'm trying to follow the method here to 'Scrapy' data from the web and simultaneously save that data directly to my Django database using Scrapy's item pipeline.
However, when I try to run scrapy crawl spidername, I'm getting the error:
ImportError: No module named django.core.management
At first I thought it was because my Scrapy project was outside of my Django project folder, but even after I moved the whole project into my Django project folder I kept getting the same error. If I open a python shell inside the Scrapy project folder in its new location (inside my Django project folder), import django.core.management works fine. So what's going on?
EDIT: Some additional info: I'm doing this on a Webfaction server, and the path to my Django project is /home/gchorn/webapps/django_app/django_project. I'm using Django version 1.4.1, Scrapy 0.16 and Python2.7. The layout of the Django+Scrapy project is as follows:
django_project/
__init__.py
manage.py
settings.py
urls.py
myproject #folder containing wsgi.py
app1
app2
app3
templates
ScrapyProject/
scrapy.cfg
ScrapyProject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
my_spider.py
Try setting this in your Spider's settings.py:
import os
import sys
sys.path.append('/home/gchorn/webapps/django_app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Then you can import your model classes like:
from django_project.app1.models import some_model

Error: No module named fileupload

I'm attempting to use this ported version of jQuery File Upload (https://github.com/sigurdga/django-jquery-file-upload). I placed the 'fileupload' directory in the root of the project (next my myapp, settings.py) and modified my urls.py file with url(r'^upload/', include('fileupload.urls')), and added 'fileupload', to my INSTALLED_APPS setting in my settings.py file. When I try python manage.py validate (OR shell OR syncdb) I get this error: Error: No module named fileupload
Did I install the application wrong? I have PIL installed and obviously Django (1.4.1).
If you're using Django 1.4, and you've installed the fileupload app in the same directory as the settings.py, then you probably need to use the path myproject.fileupload instead of fileupload.
# settings.py
INSTALLED_APPS = (
...
'myproject.fileupload',
...
)
# urls.py
url(r'^upload/', include('fileupload.urls'))
The alternative would be to move your fileupload app into the parent directory.

django project root self discovery

Ok so I recall there are some commands you could put in the settings.py file so that basically when you move your django project to another directory it won't get foo-bar'd up.
I know I could just do this by having a string variable everywhere it mentions the home directory but is there a more elegant way of doing this?
The architecture of a project in Django
root/
app1/
app2/
...
main/
settings.py
Inside settings.py:
SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) -> gives the path of the file settings.py: root/main/. This is NOT THE ROOT OF THE PROJECT
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__)) -> gives the root of the project: root/. This is THE ROOT OF THE PROJECT.
Django 1.8 already includes the project root directory as BASE_DIR:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
And you can use it in your app by importing settings:
from django.conf import settings
...
...
print(settings.BASE_DIR)
Grab the __file__ global, and use the various functions in os.path on it.
import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))