I'm trying to serve static files on a Django project with DEBUG = False. I found the WhiteNoise project and configured it according to the docs, and my project works great when deployed to Amazon Web Services. However, when I try to run the project locally, the static files aren't served. I've tried running python3 manage.py runserver --nostatic and also adding 'whitenoise.runserver_nostatic' in INSTALLED_APPS per the docs here. The static files still aren't being served locally, though. Does anyone know if there are some other settings or configurations I should be changing?
Related
Static files load when DEBUG=True (locally and at the dev server), but not when DEBUG=False (production).
STATICFILES_DIRS is set to my dist dir created by Vue, and dist is not in the .gitignore. Heroku runs collectstatic on every deploy by default (I have not changed this).
The actual error is a 404 when trying to load any static file. The whitenoise package is being used. I've updated the middleware settings, and wsgi.py according to the docs, and have the settings variable which enables compression via whitenoise set (also according to the whitenoise docs).
whitenoise usually works fine with other apps. I'm not sure what's wrong with this. The difference is that I'm using Vue for the first time. I'd never used a js framework before.
When setting DEBUG=False locally, staticfiles still load fine, so I am unable to debug in that way.
Can anyone help?
django-heroku was the problem. In my settings I was using django-heroku. Removing it got staticfiles to load at production.
I've built a Django project with Python and a MySQL database. I'm ready to deploy it to a shared server hosting platform called Hostgator. Their tech support tells me to load all my project files directly into a public_html directory, but when I do that, and navigate to my domain, I just see a list of files (see below), instead of the website I built. What am I missing?
I can't find any good documentation for this kind of deployment. I've done the Django deploy checklist and I think I have that stuff done right. I'm wondering about if/what to put in an .htaccess file, and I'm also not sure how to configure my STATIC_URL or STATIC_ROOT. Do I need to update those to have the path of my production domain? I have run the collectstatic command on the project.
As of now, I have the following for my static file handling in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
You don't want your django app in public_html. If you are using nginx or apache (you are using one of them, probably visible in the lower left of your screenshot just out of crop range), you likely want to proxy to the process running your Django app (gunicorn is one way to do that).
Essentially, Nginx handles all the web traffic, and hands off (via proxy) anything for your Django app to Gunicorn which is running your wsgi application (Django). Nginx can also then serve up your static files as well.
Digital ocean has a decent 'how to' that covers most of it in depth.
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
I have configured my django application on webfaction, and it is working fine as of now. I wanted to know how to serve my static and media files on apache. Django has a development server which serves these files. I had initially setup apache on my local environment and added many lines of script to serve static and media files via apache.
However, while doing the same on webfaction, as per the webfaction docs I didn't have to do much and that got me thinking that there might be more steps needed to serves these files via apache.
Therefore, I wanted to confirm, are my static and media files running from apache or not? I am afraid that this is the case here as well because I have not yet configured apache to serve these files since in development environment django serves the static and media files are served by django itself.
A typical Django deployment at WebFaction involves at least two application instances:
A Django app instance for the version of Django you're running
A Symbolic Link to a static-only app instance
The Symbolic link to a static-only app expects you to enter the absolute path for the origin of the symlink. This should point to the directory specified by STATIC_ROOT.
Given an project structure of:
/my-project/
/my-project/
settings.py
...
/static_assets/ (used for local development. Not typically deployed.)
/static/ (used for production)
This would translate to a directory structure at WebFaction like:
/home/
/your-account-name/
/webapps/
/your-django-app-name/
/apache2
/bin
/your-django-app-name (synonymous with "my-project" above)
/static/ (this is what your 'static' symlink path)
/lib
/pythonx.y (modules go here. Django lives here)
When you create your "Website" instance, you will specify your Django app instance to run on /, and your Symbolic link app instance on the path specified by STATIC_URL, typically /static.
You should run with DEBUG = False at WebFaction, which in turn, causes django.contrib.staticfiles to not serve any files that are located in STATICFILES_DIRS.
Hope that helps you out.
I have a Mac running OS X 10.9.3. I am trying to setup a Django application backed by a PostgreSQL database served by gunicorn, with static assets served by NGINX. I'm an old hand at Django with MySQL running with the developement server (manage.py runserver). But I'm new to setting it up with virtualenv, gunicorn and NGINX. So I'm following the instructions here.
My Django Project is being served successfully at http://localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited http://localhost:3026/admin/. I have included a screenshot below.
Why does this admin page look so ugly? It lacks the neccessary graphical interface and css that it is supposed to have? It looks like NGINX is not properly serving up those static assets. How can I troubleshoot and fix this issue?
I even did python manage.py collectstatic. That went and successfully copied all the static files to where they were supposed to (I think?) live in /opt/myenv/static. You can see the output of that command here. I then re-started gunicorn and nginx. I thought that would fix it. But unfortunately it didn't. The issue remains. In my Django settings.py file, I have configured the STATIC variables as follows:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
)
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Static files are only served by Django when the debug server is being used. When the site is being handled by a separate web server you need to configure the web server to serve the static files itself.
Actually, it is recommended to use Django's own development server(python manage.py runserver) while you are on localhost(from my years' experience with Django and virtualenv). Since it save your precious time configuring the nginx locally.
However, if you do want to use Nginx to serve you static files locally, you may want to copy django's contrib **/admin static file directory to where your virtualenv points to**. Your virtualenv file(like a *.nginx file) of nginx will look like this:
server_name localhost;
location /static {
alias /path/to/where/your/admin/static/directory/is;
autoindex on;
access_log off; #optional
}
Then restart nginx you probably will get it working.
PS: for more about installing and configuring Nginx locally on a Mac, you may want to check this out.
I'm deploying my app and I wonder what I'm missing.
I did the following:
Set my STATIC_ROOT to an empty folder in my server.
Set the STATIC_URL to '/static/'
Added 'django.contrib.staticfiles' to INSTALLED_APPS
In development my static files are in the root of my app in a folder named static.
So, I ran manage.py collectstatic and all my files were copied to my static_root.
However, it doesn't work. I don't know if i'm missing any step.
Any help would be great
Thanks
. 4. Point Apache at your static folder.
As explained in Django's documentation, Django serves static files itself in development only, when deploying your application in production, it's up to you to make your web server (apache, lighttpd, nginx, whatever) serve the static files.
Django's documentation provides instructions for doing so with Apache here
Django while it's not in debug mode should not serve static files, for performances reasons, you should use:
./manage.py collectstatic
then configure your web server (apache or nginx) to serve this folder to the right url.