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?
Introduction. Uvicorn is an ASGI web server implementation for Python. Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.
As well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.
The main thing you need to run a FastAPI application in a remote server machine is an ASGI server program like Uvicorn. There are 3 main alternatives: Uvicorn: a high performance ASGI server.
For a typical Django project, invoking Uvicorn would look like: This will start one process listening on 127.0.0.1:8000. It requires that your project be on the Python path; to ensure that run this command from the same directory as your manage.py file. For more advanced usage, please read the Uvicorn documentation.
To reap all the benefits of this feature, you have to run Django under an ASGI server like Daphne, Uvicorn, or Hypercorn. In this guide I use Uvicorn. The stack uses battle-tested components:
To test the socket activation mechanism, you can send a connection to the socket through curl by typing: You should receive the HTML output from your application in the terminal. This indicates that Gunicorn was started and was able to serve your Django application. You can verify that the Gunicorn service is running by typing:
The traditional way to deploy a Django app is with a Web Server Gateway Interface (WSGI). However, with the advent of Python 3 and the support of asynchronous execution, you can now execute your Python apps via asynchronous callables with an Asynchronous Server Gateway Interface (ASGI).
When not running with the built-in development server, you'll need to either
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With