Stylesheets and sqlite in Django/Openshift - python

I've got an Openshift/Django issue:
I'm having difficulty getting stylesheets and my SQLite database working in Openshift. When attempting to access a page that uses the database, I get the following Django debug message: "no such table: mytable". I get the same in rhc tail. Everything else appears to work.
My local file setup:
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here
mydatabase.db
For openshift, I modify the directory as follows:
openshift appname
wsgi
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here (NOT WORKING)
mydatabase.db (NOT WORKING)
Relevant bits of my setup.py file:
BASE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
STATIC_PATH = os.path.join(BASE_DIR, 'static')
DATABASE_PATH = os.path.join(BASE_DIR, 'mydatabase.db')
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATABASE_PATH,
}
...
STATIC_ROOT = ''
STATIC_URL = '/static/'
Any ideas? It works fine when running locally.

You'll want to checkout this blog post that goes over database configuration with django.
https://www.openshift.com/blogs/rapid-python-and-django-app-deployment-to-the-cloud-with-a-paas

You are likely getting the "no such table" error because the table does not actually exist (or the database itself does not exist). You do not mention running python manage.py migrate (or for Django before version 1.7 python manage.py syncdb). Without having run that command the table will not be automatically created.
In your case I think the database does not exist. Every time you do a git push to openshift, all files in your account will be overwritten except the special folder referenced in $OPENSHIFT_DATA_DIR. This is the only folder that is guaranteed to be saved and persisted across updates. You may have gone through the trouble of running migrate before and then on your next git push the database was destroyed because you had it set to be stored in your project directory. To ensure that your sqlite database is persisted across pushes you should update your settings file as follows:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.environ['OPENSHIFT_DATA_DIR'], 'db.sqlite3'),
}
}
...
On Openshift you need to use action hooks to run the migrate command. This is done by creating a folder called .openshift/action_hooks inside of your project folder. Within the folder create an executable bash script called deploy.
project
.openshift
action_hooks
deploy
project
app
templates
static
The deploy file should contain the migrate and collectstatic commands. migrate will create the tables and collectstatic will copy all of the files in your static folder to the location specified in your settings under STATIC_ROOT. When running with DEBUG=false Django will not serve static files automatically. Openshift will use apache to serve the static files that are stored in wsgi/static so you should move the static folder up one level on Openshift and set STATIC_ROOT to point there. This should solve the stylesheet problem.
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi/static')
project/.openshift/action_hooks/deploy:
#!/bin/bash
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR
echo "Executing 'python manage.py migrate'"
python manage.py migrate
echo "Executing 'python manage.py collectstatic --noinput'"
python manage.py collectstatic --noinput

Related

Django static file not loading. Bad error message

I tried loading style.css in static file but it wouldn't work. I made sure to configure STATIC files in settings.py like this
STATIC_URL = '/static/'
STATICFILES_DIR = [str(BASE_DIR.joinpath('static'))]
All my paths are correct and I loaded static. I cleared my cookies and the CSS still wouldn't work.
if you are running this on django's built-in server then one thing you can do is to make sure your static files folder is in the same directory as the manage.py file because when we run the server as python manage.py runserver the relative path for everything else starts from the directory where manage.py is

Why am I getting an internal server error when deploying Django with Heroku (admin page still works)?

I am trying to deploy my Django website with Django with Heroku, and it keeps showing me "Internal Server Error." None of the other answers regarding this same problem here at SO solved my problem, and I noticed that I only have this problem when I set DEBUG to False.
My heroku logs commands show me the following error:
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for '/images/posting/bike.png'
I have setup my settings the following way:
ALLOWED_HOSTS = ["stratagembetaapp.herokuapp.com"]
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
django_heroku.settings(locals())
Additionally, gunicorn, django-heroku, and whitenoise are perfectly installed. My requirements.txt and Procfile are also in order.
I have also already run the "python manage.py collectstatic" in the Heroku shell, but I still get the same result.
The error is that you are referencing a static file in your templates which doesn't exist (or isn't in the right place). In DEBUG mode this doesn't generate an error, but in production it does.
Somewhere in your templates you have a reference like this:
{% static '/images/posting/bike.png' %}
I think the error here is just that leading slash, so possibly it will work if you just use {% static 'images/posting/bike.png' %}.
when you deploy to heroku successfully but Application error or internal server error
do the following !
1 with debug=False every thing works fine in development environment
2 make sure you did not import unnecessary packages .
3 check your code very carefully
4 run python manage.py collectstatic
5 after deployed on heroku make sure to run
heroku run python manage.py migrate
6 if you deployed successfully and it does not work .the problems are definitely minor errors in your code check them on dev server with debug=False and make sure every thing worksalso in dev server with debug=False make sure to run
python manage.py collectstatic
Are you using a static file server? From your settings, it doesn't look like you are using one. Heroku won't store your static files. These tutorials can walk you through the process. Storing your files on s3 isn't free but it is super cheap (First 50 TB/ month $0.023 / GB)
https://www.codingforentrepreneurs.com/blog/s3-static-media-files-for-django/
https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/

django collectstatic does not exclude external folder

I located my client project folder and django project folder under the root folder separately. I configured django settings.py to contain client's app folder to serve in development and dist folder to be gathered by collectstatic
STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/app"), # in development
os.path.join(BASE_DIR, "..", "Client/dist"), # to be gathered by collectstatic
]
Client/app contains original js/css/html files for development and
Client/dist contains concatenated and uglified files for production.
Because Client/app folder is only for development, I want to exclude the folder when I use collectstaic command.
However, collectstatic -i app does not exclude Client/app folder. I tried
collectstatic -i Client/app
collectstatic -i ../Client/app
collectstatic -i app*
but, nothing did work.
How can I exclude folder outside django directory?
You would not do that normally. You would define a different STATICFILES_DIR depending on what environment you run.
Very basic idea:
if DEBUG:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/app")]
else:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/dist")]
Instead of relying on the DEBUG setting though, I'd recommend you use a separate config file for each. You then choose which to run when you invoke Django.
For instance, assuming this file tree:
settings/
├ __init__.py # empty
├ dev.py
└ prod.py
…you'd start Django this way:
export DJANGO_SETTINGS_MODULE="settings.dev"
./manage.py runserver
To avoid repeating shared configuration, create a common.py and import it from both dev.py and prod.py, using from settings.common import * (probably the only use case where it's permissible to import *).
Though it does not technically answer your question, I think this is a cleaner approach to the wider problem of handling environment-specific configuration.

What is the ideal directory structure for Django deployed app on openshift?

I am successfully deploy my application in open shift with first hello message but now
I want an ideal directory structure for deploy on openshift this Thanks
Check the django-example repo from Openshift. You have an example project from where you can get the project structure.
project
appname-1/
appname-2/
appname-n/
static/ # to store your static files
project/
wsgi.py # file created by django-admin startproject
templates/
wsgi/
static/ # in your settings.py set this folder as your STATIC_ROOT
manage.py
setup.py # file created by openshift python cartridge
wsgi.py # file created by openshift python cartridge
When you do a git push to your openshift remote, all of these files and folders will be created in $OPENSHIFT_REPO_DIR. The special folder $OPENSHIFT_REPO_DIR/wsgi/static is used to bypass wsgi and serve the files directly using apache. For this reason you must set this folder as your STATIC_ROOT to collect all of your static files.
Also for storing user uploaded files you should create a folder under $OPENSHIFT_DATA_DIR. For example, $OPENSHIFT_DATA_DIR/media. This folder will be persisted across future deploys.
If you use sqlite3 for you database your settings should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.getenv($OPENSHIFT_DATA_DIR), 'media'),
}
}

Django and django oscar

Location of django:
/usr/lib/python2.7/dist-packages/django/__init__.pyc
Location of django oscar:
/usr/local/lib/python2.7/dist-packages/oscar/__init__.pyc
My static files are not getting served properly. Above is my production setting. On my local machine, the locations are:
/usr/local/lib/python2.7/dist-packages/oscar/__init__.pyc
/usr/lib/python2.7/dist-packages/django/__init__.pyc
Could this be a possible reason for above problem?
Oscar ships its own set of static files in oscar/static/oscar When you deploy your site, you should run manage.py collectstatic so these files are also collected in your STATIC_ROOT
On DigitalOcean's Django app, your Nginx configuration is located in /etc/nginx/sites-enabled/django You may need to update the following section to point to the location of your STATIC_ROOT
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}

Categories

Resources