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.
Related
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'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?
I am developing a Django application and I want to put it online on DigitalOcean so I can show it to a remote future user. I am quite new to django and python and I have never in my life touched the deployment of web applications, so I'm a bit lost in all that.
I used the one-click install of a django droplet in digitalocean, which uses nginx and gunicorn. I was able to make the app load in the browser using the ip address (159.203.58.210), but the problem is that no static files can be loaded.
I googled that for some days and nothing could help me. I'm guessing it has something to do with either permissions not allowed or that I did not write the static files correcly in my templates and *.py files.
I'm running my application with the command:
python manage.py runserver localhost:9000
I'm using sqlite3 as db
My complete code is here:
https://github.com/gbastien1/carte-interactive
On the server, my app is at /home/django/international
(where international is my site name, the app is called carte_interactive)
My staticfiles settings in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT= '/home/django/international/carte_interactive/static'
my staticfiles settings in /etc/nginx/sites-enabled/django.conf (same for media)
location /static {
alias /home/django/international/carte_interactive/static;
}
In my templates, I use {% load staticfiles %}
And I call my static files like so:
{% static 'carte_interactive/css/style.css' %}
And in my .py files, I sometimes call those files like this, which I don't know if it's right:
app_name='carte_interactive'
json_data_url = static('carte_interactive/json/data.json')
json_data_file = open(app_name + json_data_url, 'w') <-- here
It was the only solution that worked on development to access my static files in views.py, but it might be a problem in production with collectstatic and all, I don't know.
I am using django 1.9.4 on server and 1.9.1 on development it seems.
Do you have any idea why my staticfiles are 404 not found on the server but everything works fine on development?
Could there be a permission issue with the folder /home/django/... for the browser to reach my static files?
Is there a better way to call my staticfiles in views.py?
Thanks in advance!
edit:
The console error I get for all my template static files:
http://159.203.58.210/static/carte_interactive/css/style.css Failed to
load resource: the server responded with a status of 404 (Not Found)
I assume that you don't use nginx to serve static assets in
development? Runserver can serve static files, but very much slower
than nginx, which becomes a problem once you have more than a single
web site visitor at a time. You can remove the nginx alias for static
and reload nginx to let runserver serve the files, to confirm whether
it's a problem in the nginx config.
HÃ¥ken Lid
I removed nginx and made the Django server load the static files, and now I can show it to my future users. This answered my question, though it did not solve the problem itself! Thanks anyway !
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'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.