Whitenoise to serve static files locally - python

As described in their docs here, I am configured my application to serve static files locally.
The only problem I am facing is that I am unable to determine if it is django or whitenoise which is serving static files?
Steps which I followed:
pip install whitenoise # install whitenoise
pip install brotlipy # install brotlipy for compression
INSTALLED_APPS = [
# default django apps
'django.contrib.messages',
# REMOVE IN PRODUCTION
# See: http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
# other apps
]
# add white-noise middleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
# static files serving
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# other middlewares
]
# add staticfiles
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# run collecstatic
python manage.py collectstatic
# restart the server
python manage.py runserver
# This gives me following
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/css/print.css HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/chartkick.js HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/js/jquery_pre.js HTTP/1.1" 304 0
However, I expect something like this,
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/css/print.636363s6s.css HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/chartkick.2623678s3cdce3.js HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/js/jquery_pre.6276gdg3js8j.js HTTP/1.1" 304 0
How can I check if whitenoise is working and that it is serving static files?

Since it's been quite a while since you asked this question, I imagine you've found an answer by now. But if anyone else stumbles on it, I've found that Django won't serve static files if you have DEBUG=False. So if you have that setting and you're still seeing static, it's likely being done by whitenoise.

Related

How to run Django with Uvicorn webserver?

I have a Django project running on my local machine with dev server manage.py runserver and I'm trying to run it with Uvicorn before I deploy it in a virtual machine. So in my virtual environment I installed uvicorn and started the server, but as you can see below it fails to find Django static css files.
(envdev) user#lenovo:~/python/myproject$ uvicorn myproject.asgi:application --port 8001
Started server process [17426]
Waiting for application startup.
ASGI 'lifespan' protocol appears unsupported.
Application startup complete.
Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
INFO: 127.0.0.1:45720 - "GET /admin/ HTTP/1.1" 200 OK
Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/base.css
INFO: 127.0.0.1:45720 - "GET /static/admin/css/base.css HTTP/1.1" 404 Not Found
Not Found: /static/admin/css/dashboard.css
Not Found: /static/admin/css/dashboard.css
INFO: 127.0.0.1:45724 - "GET /static/admin/css/dashboard.css HTTP/1.1" 404 Not Found
Not Found: /static/admin/css/responsive.css
Not Found: /static/admin/css/responsive.css
INFO: 127.0.0.1:45726 - "GET /static/admin/css/responsive.css HTTP/1.1" 404 Not Found
Uvicorn has an option --root-path so I tried to specify the directory where these files are located but there is still the same error (path is correct). How can I solve this issue?
When not running with the built-in development server, you'll need to either
use whitenoise which does this as a Django/WSGI middleware (my recommendation)
use the classic staticfile deployment procedure which collects all static files into some root and a static file server is expected to serve them. Uvicorn doesn't seem to support static file serving, so you might need something else too (see e.g. https://www.uvicorn.org/deployment/#running-behind-nginx).
(very, very unpreferably!) have Django serve static files like it does in dev
Add below code your settings.py file
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
Add below code in your urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [.
.....] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then run below command but static directory must exist
python manage.py collectstatic --noinput
start server
uvicorn main.asgi:application --host 0.0.0.0

Using Flask with Google App Engine Dev Server, swagger ui not found?

I am developing a website using Flask and Flask Restplus. I tried to use Google App Engine to deploy the app, however when I run dev_appserver.py on my computer, all the swagger-ui files cannot be found. I have no ideas why this happens? Can anyone tell me how to solve this?
Here is the example logs 404 for the resource:
INFO 2017-06-14 04:21:52,859 module.py:809] default: "GET /api/ HTTP/1.1" 200 3710
INFO 2017-06-14 04:21:53,113 module.py:809] default: "GET /swaggerui/bower/swagger-ui/dist/css/screen.css HTTP/1.1" 404 233
INFO 2017-06-14 04:21:53,113 module.py:809] default: "GET /swaggerui/bower/swagger-ui/dist/lib/jquery.slideto.min.js HTTP/1.1" 404 233
INFO 2017-06-14 04:21:53,115 module.py:809] default: "GET /swaggerui/bower/swagger-ui/dist/lib/object-assign-pollyfill.js HTTP/1.1" 404 233
INFO 2017-06-14 04:21:53,121 module.py:809] default: "GET /swaggerui/bower/swagger-ui/dist/css/typography.css HTTP/1.1" 404 233
You need to put swaggerui inside your project. I suggest making a libs directory at the root of your project (same level as app.yaml).
Then, use GAE's vendoring method. To use vendoring, create (or modify) appengine_config.py in the root of your project, as follows:
from google.appengine.ext import vendor
# Add 3rd party libraries in the "libs" folder.
vendor.add('libs')

Django Static Files Not Being Found (debug off)

I cannot get Django to correctly reference my static files when debug is off. I know there are many other posts on this site about this, but none of them have fixed my issue.
My directory tree is like this:
└── project
└── app1
└── app1
├── manage.py
└── project
└── project.sock
└── projectenv
└── static
└── template
In my settings.py I have the following:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
In my nginx configuration file I have the following:
location /static {
autoindex on;
alias /home/user/myproject/static/;
}
running ./manage.py collectstatic correctly places all my files into the static directory.
however, in running my server, all of these static files 404:
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/jquery/jquery.min.js HTTP/1.1" 404 6418
[22/Jul/2016 17:05:31] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 404 6418
The static files are placed in the same directory as my manage.py file. What am I doing wrong?
Adding this in my urls.py does work, but aren't I supposed to serve them directly from nginx instead?
if settings.DEBUG is False: #if DEBUG is True it will be served automatically
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
It's slower since static files rendering goes through Django instead
served by your web server directly
You should configure your nginx: edit nginx.conf the following way: add this:
location ~* ^(/static/|/media/).+.(jpg|jpeg|gif|png|zip|eot|woff|woff2|svg|ttf|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
root /path/to/parent/folder/of/static/folder;
}
to the server { clause corresponding to your website, then restart nginx and it should fix your problem, if not - check nginx logs, make sure that the folder has appropriate permissions for nginx user/group etc

Problems with Django static files

I have downloaded this project and trying to make it work: http://sourceforge.net/p/etconf/git/ci/master/tree/
The problem is, after I managed to install all prerequisites, it runs, but does not load staticfiles. According to settings it should look for them in the /media directory, but however I tried to change MEDIA_ROOT, STATIC_URL and various different parameters in settings.py it shows in the console:
[09/Oct/2015 10:56:08] "GET /static/configurator/admin/css/base.css HTTP/1.1" 404 2281
[09/Oct/2015 10:56:08] "GET /static/configurator/configurator.css HTTP/1.1" 404 2275
[09/Oct/2015 10:56:08] "GET /static/configurator/prototype.js HTTP/1.1" 404 2263
[09/Oct/2015 10:56:08] "GET /static/configurator/configurator.js HTTP/1.1" 404 2272
What am I doing wrong? Maybe it's some sort of old bug or something (project is using python 2 and django 1.1)
I guess you missed to run below command after configuration:
python manage.py collectstatic
What I see is you get 404 error for static files which means that the files are not located where it is been searched for.

Django not picking up new changes

I'm running an old branch A from my GitHub repo, and Django is not picking up any of the static files. What's weird is that if I make any text changes to the templates, it doesn't pick those up either. I've tried refreshing the page, opening Chrome in Incognito, restarting the server, etc. -- nothing is working.
After running the development server in a different branch B, and switching back to this one, Django acted as if I was still running on branch B (got not 404 errors):
$ ./manage.py runserver
0 errors found
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[12/Sep/2012 23:25:48] "GET /interviews/ HTTP/1.1" 200 13295
But as soon as I restarted the server, it reverted to the un-changed version of branch A (none of the changes were picked up and no static files could be found):
$ ./manage.py runserver
Validating models...
0 errors found
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[12/Sep/2012 23:26:20] "GET /interviews/ HTTP/1.1" 200 13295
[12/Sep/2012 23:26:20] "GET /static/js/plugins.js HTTP/1.1" 404 2653
[12/Sep/2012 23:26:20] "GET /static/js/jquery-1.7.1.min.js HTTP/1.1" 404 2653
[12/Sep/2012 23:26:20] "GET /static/css/style.css?v=2 HTTP/1.1" 404 2653
[12/Sep/2012 23:26:20] "GET /static/js/script.js HTTP/1.1" 404 2653
Between the two branches, settings.py and the static folder are identical so I'm not even sure where to begin with troubleshotting.
DEBUG = True
Check this. Built-in Django dev server does not work with static files if DEBUG = False.

Categories

Resources