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 !
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
A Django-based service that I'm working on allows users to upload media files via REST API or Django admin but does not provide or use any static files (like css styles, js libraries, etc.).
Media files are stored in specific fields in database and use S3 bucket as storage backend so server itself does not directly serve any files at all.
Having such a case is running collectstatic command required every time application is being deployed?
Thought the concept of static and media files in Django application is rather simple I'm still confused about whether configurations for them should be somehow related?
As Django won't serve the staticfiles when the DEBUG is False (through deployment), if you want the css, js and other static files (at least in the admin, in your case), you need to run collectstatic.
So if you want django admin to be like your development environment (with css and images), you need it.
Your Django-based service does not need collectstatic at all.
Question is about django admin - because admin app uses static content css/img/js.
Do you use django admin in production?
If yes - then you need to run collectstatic command it, otherwise admin site will be unstyled or totally broken because of missing js files.
If no - then you collectstatic command has no use for you.
I'm trying to put a django website live and after going through the deployment checklist and turning debug = False django gives out a 404 error and only static files are loaded no media files uploaded from users are displayed. However with debug=True all the images and files work properly. below are the configurations of the project with debug = True. The console logs show multiple missing files.can someone point to me what i'm doing wrong?
settings.py
urls.py
console errors
Basically you are putting the application in DEBUG=False "mode", this means that django won't serve any static files. But on the other hand, if you want to test locally how DEBUG=False affects your environment, locally you can use python manage.py runserver --insecure option which will allow django to serve static files. But I must note that this is vastly inefficient and maybe insecure too.
Hope this helps.
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.