During development I like to deploy Django static files in an as close to production as possible setup. To achieve this I wrap the Django backend into an image ( Dockerfile
) and frontend JS and backend Django static files into another image together with nginx
configured as webserver ( Dockerfile_nginx
). The setup is as follows:
File system structure:
<projekt-repo>
/frontend
/backend
/static (generated with python manage.py collectstatic)
settings.py
manage.py
nginx.conf
Dockerfile
Dockerfile_nginx
settings.py
:
STATIC_ROOT = os.path.join(BASE_DIR, "backend/static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
nginx.conf
:
server {
listen 0.0.0.0:8080;
root /var/www;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 0.0.0.0:8000;
root /var/www/django;
location /static/ {
autoindex on;
alias /var/www/django/static/;
}
}
Dockerfile_nginx
:
FROM nginx:1.17.8-alpine
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
COPY edge_frontend/www /var/www
COPY edge_backend/static /var/www/django/static
If I run the application and try to login via the Django admin site ( localhost:8000/admin
) the site is not styled properly and the log output states
backend | Not Found: /static/admin/css/base.css
backend | Not Found: /static/admin/css/login.css
backend | Not Found: /static/admin/css/responsive.css
backend | Not Found: /favicon.ico
backend | Not Found: /static/admin/css/base.css
backend | Not Found: /static/admin/css/login.css
backend | Not Found: /static/admin/css/responsive.css
backend | Not Found: /favicon.ico
Obviously there is some mismatch in the setup which I am not able to spot right now? Can someone help?
create a folder in your project with the name "static" and then
add in your settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
or you want another path?
You have to set BASE_DIR
, STATIC_URL
and STATIC_ROOT
in settings.py
as below...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And run python manage.py collectstatic
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.