Collecting django static files for deployment - django

I developed a django application to handle requests from angular application. Now, should I collect static files for django application while deploying it?
python manage.py collectstatic
Will my django respond to the requests without staticfiles? Because, I found in many documentations that collectstatic is being performed in the steps to deploy django application.

It depends on how you are going to run your Django application. If you are putting it anywhere public, you should have Debug set to False in your settings.py and as such you will need to do the collectstatic step, but not necessary every time you make a change; only if you've added more static files.
The 'Serving the files' section of the documentation (https://docs.djangoproject.com/en/3.1/howto/static-files/) is clear on using runserver in production:
This method is grossly inefficient and probably insecure, so it is
unsuitable for production.
Determine what sort of volume your site is going to have to decide if you want something like nginx being your webserver and proxying requests to your Django app being run by Daphne, Gunicorn or uvicon while nginx (or other fav web service) serves up your static content. If it is too much for the one connection or the server it may make sense to host your static files elsewhere - it really all depends on your use case.

Related

Do I have to run collectstatic every time to serve images when they are uploaded by user?

I have a web app that takes uploaded images and displays it to the user after registration. I have the media and static files separated in respective folders. When I run the app with debug as True, the images are served up to be displayed, but when debug is off, the images don't show. I have tried having the media folder under the static folder and running collectstatic and the images show. But when new images are uploaded, they are not served until the collectstatic is run again. Do I have to keep running collectstatic or there is better to have these folders separated and the images still show??
Yes, these folders should be separated. In Debug mode runserver serves these data for you:
During development, if you use django.contrib.staticfiles, this will
be done automatically by runserver when DEBUG is set to True (see
django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
In production (DEBUG=False) you need to use more suitable tools, for instance, gunicorn in combination with nginx. Check out this guide to understand how to properly set up your workflow in production.

Divio Live server not serving the static files when DEBUG=False

I am running a website in Divio Cloud. My Test server serves the static files fine but my Live server gives 404. When I switch DEBUG to True on the Live server it starts serving the static files fine.
When the Live server starts, it has this in the log:
[uwsgi-static] added mapping for /static/ => /app/static_collected
Shouldn't that take care of serving the static files correctly?
--edit--
More info. I actually didn't have any "normal" static files. I added a static file using {% static %} and it is working! The files that are not working are collected with django-sass-processor and django-compressor. I have in settings.py:
STATICFILES_FINDERS.extend([
'compressor.finders.CompressorFinder',
])
Shouldn't this only affect when the files are collected?
I think the issue is most likely in the fact that you are using Django Compressor. There are various different ways in which to use Django Compressor.
In its documentation, please see Offline Compression. This is what you need for it to work on Divio Cloud.
For future reference:
In Divio environment to get the compressor based processors (in this case it was django-sass-processor) to work is add something along following lines in the end of the Dockerfile:
RUN DJANGO_MODE=build python manage.py compilescss
# <STATIC>
RUN DJANGO_MODE=build python manage.py collectstatic --noinput
# </STATIC>
# Remove the css files in development environments (in Live they
# are already collected)
RUN DJANGO_MODE=build python manage.py compilescss --delete-files

How to correct Django server from loading basic html on web browser instead of standard html?

How can I correct the basic html display I get when I load the Django admin page, just as shown in the snapshot attached?
This is because your static files aren't set up properly. Typically Django's development runserver will take care of this for you, but in production, you need to run python manage.py collectstatic after ensuring your static files settings are correct: https://docs.djangoproject.com/en/1.11/howto/static-files/#configuring-static-files
Good luck!

File uploads in Heroku deployment with Django

So I was finally able to set up local + prod test project I'm working on.
# wsgi.py
from dj_static import Cling, MediaCling
application = Cling(MediaCling(get_wsgi_application()))
application = DjangoWhiteNoise(application)
I set up static files using whitenoise (without any problems) and media (file uploads) using dj_static and Postgres for local + prod. Everything works fine at first... static files, file uploads.
But after the Heroku dynos restart I lose all the file uploads. My question is, --- Since I'm serving the media files from the Django app instead of something like S3, does the dyno restart wipe all that out too?
PS: I'm aware I can do this with AWS, etc, but I just want to know if thats the reason I'm losing all the uploads.
Since I'm serving the media files from the Django app instead of something like S3, does the dyno restart wipe all that out too?
Yes!. That's right. According to the Heroku docs:
Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code.
See, also this answer and this answer.
Conclusion: For media files (the uploaded ones), you must use some external service (like S3 or something). whitenoise is just for static files. See here why whitenoise is not suitable for serving user-uploaded (media) files.

Why won't newly installed Django app with NGINX serve static assets properly?

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 localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited 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?
EDIT:
After I posted this question, I 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:
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Try run command,
python manage.py collectstatic
If the commands executes successfuly, the static file would be generated in your project path, and then if you config the right static path, the web page will be correct.