#WSGI vs. ASIG static files not found in app

39 messages · Page 1 of 1 (latest)

half lodge
#

II have a very basic django project running. However, the django project itself is not at the top Python package root but in a subfolder. It took me quite some time figure out how to adjust all the variables, that django is satisfied with all folder look-ups.

Next I switched from WSGI (python manage.py runserver) to a uvicorn ASGI application.

... and requesting any static files raise a 404 all of a sudden.

In short: requesting static files from WSGI is fine, same for ASGI is not.

I did not tweak STATIC_ROOT. django.contrib.staticfiles is part of the INSTALLED_APPS.

STATIC_URL = 'static/'

The static files or in the app's static subfolder.

What do I have to tweak extra for ASGI to hand out static files like WSGI does? What did I miss?

#

BTW: django 4.2.6

fleet flint
#

How are you serving your static files?

half lodge
#

I read that one and I tried with and without STATIC_ROOT to no avail.

#

Currently I'm again back at the beginning, placing all my static files in the app's static folder.

fleet flint
#

Are you doing this locally or in production?

half lodge
#

locally

fleet flint
#

Okay, then static root shouldn't matter.

#

STATIC_URL should have a leading and trailing slash though.

half lodge
#

Mine: STATIC_URL = 'static/'

fleet flint
#

'/static/'

half lodge
#

But it works fine with WSGI...

#

Ok...

fleet flint
#

Is DEBUG = True? And are you including the static urls?

fleet flint
#

If you use any relative URL without a leading slash in a href, the browser will interpret that to mean to append the path onto the current URL. Meaning testserver + static/ = testserver/static/, but testserver/other + static/ = testserver/other/static/

#

Does the admin appear styled?

half lodge
#

Tried /static/ .... nope.

#

Works fine with WSGI, but gives a 404 on ASGI with uvicorn.

fleet flint
half lodge
#

To get full ASGI benefit.

fleet flint
#

Okay, so you're running this in production mode then, but locally.

#

runserver handles serving the static content for you. If you're using uvicorn, gunicorn or any other web server to serve your django app, you need to specifically serve the static files. That's where nginx or apache typically come in. BUT, you may want to install whitenoise and use that instead

#

When you run gunicorn yourapp there is nothing to catch the static file requests and serve a response.

half lodge
#

Ok. That's new to me. Thought the static files handling middleware in Django would also catch that.

fleet flint
#

Nope, that's only for local development with runserver

half lodge
#

I see.

fleet flint
#

I agree it's annoying

#

Or I think it is. Deployment is hard with Django until you're used to it. And even then it's not easy

half lodge
#

Hm, I did a FastAPI project couple of months ago and that worked fluently out of the box with ASGI...

#

But, ok. I give whitenoise a shot.

fleet flint
#

Every project has its strengths and weaknesses. I think django's ORM and migrations are worth the inconveniences elsewhere.

half lodge
#

Thx a lot! WhiteNoise seems to do the trick.
Now I have a running WSGI and ASGI serving static files.
Only thing: with uvicorn I now get this when requesting a singel static file:

python3.11/site-packages/django/http/response.py:517: Warning: StreamingHttpResponse must consume synchronous iterators in order to serve them asynchronously. Use an asynchronous iterator instead.
fleet flint
#

Yeah, that's a known issue with whitenoise. You can check the issues on the repo to see where it's at.