Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加Https-django后,静态文件在django admin中不起作用_Django_Server - Fatal编程技术网

添加Https-django后,静态文件在django admin中不起作用

添加Https-django后,静态文件在django admin中不起作用,django,server,Django,Server,添加SSL证书后,我的静态文件不仅在django admin中工作。整个网站上的其他文件工作得很好。 我如何修复这种情况(最好不要将django管理静态文件残酷复制到我的应用程序) url.py urlpatterns = [ path('admin/', admin.site.urls), path('', include('app.urls', namespace='app')), path('media/<path>/', serve,{'documen

添加SSL证书后,我的静态文件不仅在django admin中工作。整个网站上的其他文件工作得很好。 我如何修复这种情况(最好不要将django管理静态文件残酷复制到我的应用程序)

url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls', namespace='app')),
    path('media/<path>/', serve,{'document_root': settings.MEDIA_ROOT}),
    path('static/<path>/', serve,{'document_root': settings.STATIC_ROOT}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, '../static/') 

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'app/media')
app(/etc/nginx/sites available/app)更改此文件后,django管理员静态文件停止工作

upstream app_server {
    server unix:/home/app/run/gunicorn.sock fail_timeout=0;
}

server {
    #listen 80;

    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    listen 443 ssl;
    server_name ebluedesign.online;
    ssl_certificate /etc/letsencrypt/live/ebluedesign.online/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ebluedesign.online/privkey.pem;


    server_name 101.170.18.112;

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /home/app/logs/nginx-access.log;
    error_log /home/app/logs/nginx-error.log;

    location /static/ {
        alias /home/app/app/app/static/;
    }

    # checks for static file, if not found proxy to app
    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}

server {
    listen 80;
    server_name ebluedesign.online;
    return 301 https://$host$request_uri;
}

您的url模式中不应该有任何静态/媒体/url。django静态服务器不应用于生产。这就是为什么nginx的location/static/和location/media/指向
static\u ROOT
media\u ROOT
目录。此外,您还应该
collectstatic
,以便将您的所有管理文件复制到
STATIC\u ROOT
目录中(最好是位于比项目更高的级别,即,如果您使用git,则位于git repo之外)。我有一个问题,为什么要在文档中编写向应用程序添加静态文件夹?这是为
collectstatic
准备的。它需要知道所有静态文件的位置。它将收集所有文件并将它们复制到您的静态根目录中。然后,当在模板中使用{%static%}时,它将构造关于静态根的正确URL。最后,您将web服务器配置为将静态URL(即以/STATIC/开头的所有路径)与静态根目录(即根目录中的所有路径)相匹配。对于开发,Django只直接从您定义的各种静态目录中提供文件(无需静态根目录)