Why is Django (./manage runserver) serving files I've deleted? - django

In my development environment I've deleted my static files directory (rm -rf [PROJECT_NAME]/static/), emptied my browser's cache, restarted the server and still when I load a page, the static files (my JS, CSS, etc.) are still there (e.g. http://192.168.1.100:8000/static/js/bootstrap.min.js). I can't figure out how. This happens with both runserver and gunicorn. How are they still being served and how can I stop them from being served?

Check the value of STATIC_ROOT variable in Django settings.py file.

Related

Does whitenoise really require collectstatic?

I'm exploring using whitenoise to serve static files in a Django application that's packaged in a Docker container. In the documentation it says:
As part of deploying your application you’ll need to run ./manage.py collectstatic to put all your static files into STATIC_ROOT. (If you’re running on Heroku then this is done automatically for you.)
Is that really needed? I'm not running ./manage.py collectstatic and static files are still served.
If it's not needed, is it an optimization? I'm trying to avoid having needless steps in my deployment process.
As that quote states, Whitenoise serves files from STATIC_ROOT. collectstatic collects files from STATICFILES_DIRS and any app-specific static directories and puts them into STATIC_ROOT.
But there are two situations where it will serve files without running collectstatic. The first is if your files are already in STATIC_ROOT. They shouldn't be, but it's a common mistake to set the value of STATIC_ROOT to the directory containing the source files. However, in this case it won't for example find the admin files, which would need to be collected.
The other situation is if you are running with DEBUG=True. Whitenoise inherits this value for its USE_FINDERS setting, which makes it look in the same places as collectstatic itself to serve files. Clearly, you wouldn't want to run in production with DEBUG on, but you can set this setting explicitly. But as that docs link points out, doing so will disable the caching and compression features of Whitenoise.

Django How to manage static file when you run server?

I put static files under a app directory which is related to the static files.
for example,
image.jpg is used for templates under exmapleapp. so I locate image.jpg file in a directory /project/exampleapp/static/image.jpg
In Debug=True settings, Dajngo finds static files automatically by django.contrib.staticfiles. If you put static files projoject directory /project/static/, you can set STATICFILES_DIRS = []in settings.py or add like urlspatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in urls.py(if you want to serve static files manually)
so far, is nothing problem in development right? but you want to deploy, you need to do collectstatic. I am confused from now.
This is what I thought initially.
step1 : you work and test your code in local with DEBUG = True in setting. you continously save your code in git repo.
step2 : you are ready to deploy your code in server. you clone your repo and set apache server for running django framework properly(Alias static, WSGI Daemon process). you do python manage.py collectstatic to serve static file with apache server.
step3 : you keep work in local to improve your code and apply this improvement into your code in server with test.
I got confused and got questions.
Q1 : if you do collectstatic for deployment, there will be static folder(according to STATIC_ROOT settings) with all static files which is spread around in each app folders.
do you do collectstatic in local and send only the static folder which collects all to server? or do you do collectstatic in server according to Static Alias setting in Apache server?
Q2 : Do you change DEBUG = False and ALLOWED_HOSTS =[server IP] in local and save code in git repo and pull it in server??
well, now I am confused with working during development with github and deploying it to server with github with Django settings.
Tell me if you can't understand my question clearly please.
You are using static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) for serving static files in development mode. But in production, (means DEBUG = False) it will be empty list. (static returns empty list). Then collectstatic, in you can use development or production.Its intention is to copy all static files to STATIC_ROOT.So
Q1: Everyone do collectstatic on production to serve static files through webserver(Apache, Nginx)
Q2: For production and development write different settings file. You can see here for ex How to manage local vs production settings in Django?

Apache will not serve Django static files

So I've seen alot about Apache not serving Django admin static files, but for some reason, Apache is not serving any static files. It understands and finds the templates, but no images, css, or javascript is loaded.
EDIT 2: Updated the two files to show new settings
EDIT: I added the STATIC_ROOT and I was able to collectstatic files, but it still doesn't serve them after server restart.
I've tried ./manage.py collectstatic and get this error:
ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without
having set the STATIC_ROOT setting to a filesystem path.
My settings.py file is here:
https://github.com/rchurch4/stackquestions/blob/master/settings.py
My nxt4.com.conf file is here:
https://github.com/rchurch4/stackquestions/blob/master/nxt4.com.conf
If someone could please let me know exactly how to configure this so that Apache will serve django's static files, that would be great. I'm running Ubuntu on AWS with a mysql db. The filepath to the site on the server is: /home/ubuntu/nxt4.com/nxt4/
Thanks in advance
The error message seems quite clear: you have not set the STATIC_ROOT setting, so collectstatic does not know where to put the collected files. From the looks of your httpd.conf, it seems like it should be set to "/home/ubuntu/nxt4.com/static/"

Why use Django's collectstatic instead of just serving the files directly from your static directory?

From the Django Docs:
Deployment django.contrib.staticfiles provides a convenience
management command for gathering static files in a single directory so
you can serve them easily.
Set the STATIC_ROOT setting to the directory from which you’d like to
serve these files, for example:
STATIC_ROOT = "/var/www/example.com/static/"
Run the collectstatic management command:
$ python manage.py collectstatic
This will copy all files from your
static folders into the STATIC_ROOT directory.
Use a web server of your choice to serve the files. Deploying static
files covers some common deployment strategies for static files.
What's the purpose of copying the files, why not just serve them from the directory they live in within the app?
Why not just serve your static directory? You might use more than one app, and some of your apps may not be under your control. Before the staticfiles app existed, you then had to either manually copy the static files for all apps to a common directory, upload them to your CDN, or symlink them to the document root of your web server.
The staticfiles app established a convention: put static files for each app under a static directory and let Django do the work for you.
The STATIC_ROOT can be on a different machine than the application, so copying your static files to the static root means that you can serve your static files from a different server (CDN FTW!) which you wouldn't be able to do if those files where only located within their respective app directories.

How does the admin app work without collectstatic?

I created a new django app (with dj-static), enabled the admin app, started the server and to my surprise, the css files in the admin were available even though i did not run collectstatic.
So, I created a few static files in my static directory and then when I ran collectstatic, the CLI showed the admin static files being moved to the static root folder along with my other static files.
How does the static files for admin work without collectstatic? And if it works without collectstatic, why move it to static root?
Thanks.
You don't need collectstatic for any app when you're running under the development server. collectstatic is for moving apps' static files into a central location so that they can be served with the asset server.