如何 运行 Django 与 Uvicorn 网络服务器?
How to run Django with Uvicorn webserver?
我有一个 Django 项目 运行 在我的本地机器上使用开发服务器 manage.py runserver
并且我试图 运行 在我将它部署到虚拟机之前使用 Uvicorn .所以在我的虚拟环境中,我安装了 uvicorn
并启动了服务器,但是如下所示,它找不到 Django 静态 css 文件。
(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 有一个选项--root-path
所以我试图指定这些文件所在的目录但仍然有同样的错误(路径是正确的)。我该如何解决这个问题?
当不是 运行 内置开发服务器时,您需要
- 使用 whitenoise 作为 Django/WSGI 中间件(我的推荐)
使用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).
(非常非常不喜欢!)have Django serve static files like it does in dev
在您的 settings.py 文件中添加以下代码
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
在您的 urls.py
中添加以下代码
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [.
.....] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后运行下面的命令但是静态目录必须存在
python manage.py collectstatic --noinput
启动服务器
uvicorn main.asgi:application --host 0.0.0.0
我有一个 Django 项目 运行 在我的本地机器上使用开发服务器 manage.py runserver
并且我试图 运行 在我将它部署到虚拟机之前使用 Uvicorn .所以在我的虚拟环境中,我安装了 uvicorn
并启动了服务器,但是如下所示,它找不到 Django 静态 css 文件。
(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 有一个选项--root-path
所以我试图指定这些文件所在的目录但仍然有同样的错误(路径是正确的)。我该如何解决这个问题?
当不是 运行 内置开发服务器时,您需要
- 使用 whitenoise 作为 Django/WSGI 中间件(我的推荐)
使用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).
(非常非常不喜欢!)have Django serve static files like it does in dev
在您的 settings.py 文件中添加以下代码
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
在您的 urls.py
中添加以下代码from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [.
.....] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后运行下面的命令但是静态目录必须存在
python manage.py collectstatic --noinput
启动服务器
uvicorn main.asgi:application --host 0.0.0.0