Django:连接路径位于基本路径组件之外 [英] Django: The joined path is located outside of the base path component

查看:298
本文介绍了Django:连接路径位于基本路径组件之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 10,我不知道为什么在成功收集静态文件后,当我尝试在部署模式下运行服务器(debug = False)时,会发生类似以下情况:



当我通过以下操作查找静态文件时: python manage.py findstatic /static/mysite/js/javascript.js



django.core.exceptions.SuspiciousFileOperation:联接路径(/static/mysite/js/javascript.js)位于基本路径组件(/主页/xxxx/.venvs/mysite/local/lib/python2.7/site-packages/django/contrib/admin/static)



我使用虚拟环境 mysite运行它。



settings.py 中:

  STATIC_URL ='/ static /'

STATIC_ROOT = os.path.join(BASE_DIR, static)(我也只尝试了'static ')

MEDIA_ROOT = BASE_DIR +'/ mysite / media'

MEDIA_URL ='/ media /'

我的 urls.py

  urlpatterns = [
url(r'',include('mysite.urls')),
]

urlpatterns + = staticfiles_urlpatterns()

这对我来说是新的,我也已经建立了一个django网站,并且从未发生过此类错误。为什么不考虑我的STATIC_ROOT路径?另一方面,静电收集器工作正常。而且,如果我通过调试运行服务器,那么它就像一个魅力。

解决方案

findstatic 的位置参数是您要传递给的参数 static 可以在模板中呈现特定的静态文件。您看到的错误是因为使用了绝对路径,而不是传递给模板中 static 函数的参数。您确定您不是在说什么吗?

  python manage.py findstatic mysite / js / javascript.js 

对于要用



<$ p $引用的文件p> {%static'mysite / js / javascript.js'%}

  python manage.py findstatic js / javascript.js 

对于您要引用的文件

  {%静态'js / javascript.js'%} 

供您参考:

 用法:manage.py findstatic [-h] [--version] [-v {0,1,2,3}] 
[- -settings设置] [--pythonpath PYTHONPATH]
[--traceback] [-无色] [--first]
静态文件[staticfile ...]

查找给定静态文件的绝对路径。

位置参数:staticfile






nb,django的staticfiles应用程序是用于生产中。在生产中,应该使用Web服务器来提供基于静态文件的服务。例如,对于nginx,您可以添加类似的块

 位置/ static {
root / path / to / django;
}

要调试静态文件,请检查 STATIC_ROOT 目录中运行。如果是这样,请确保将您的网络服务器配置为正确地服务于您的静态文件目录。


i'm using Django 10 and i dont know why after i collect my static files successfuly, when i try to run server in deployment mode(debug=False) it occurs me something like this:

When i look for a static file by doing: python manage.py findstatic /static/mysite/js/javascript.js

django.core.exceptions.SuspiciousFileOperation: The joined path (/static/mysite/js/javascript.js) is located outside of the base path component (/home/xxxx/.venvs/mysite/local/lib/python2.7/site-packages/django/contrib/admin/static)

I run this using a virtual env 'mysite'.

In settings.py:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static") (i tried also just only 'static')

MEDIA_ROOT = BASE_DIR + '/mysite/media'

MEDIA_URL = '/media/'

my urls.py:

urlpatterns = [   
    url(r'', include('mysite.urls')),
]

urlpatterns += staticfiles_urlpatterns()

This is new for me, i also did already a django website and it never occurs such error. Why is not considering my STATIC_ROOT path? For the other hand the collectstatic works fine. And if i runserver with a debug a true it works like a charm.

解决方案

findstatic’s positional argument is the parameter you would pass to static to render a particular static file in a template. The error you are seeing is because you used an absolute path and not the parameter you would pass to the static function in a template. Are you sure you didn’t mean something like:

python manage.py findstatic mysite/js/javascript.js

for a file you would refer to with

{% static 'mysite/js/javascript.js' %}

or

python manage.py findstatic js/javascript.js

for a file you would refer to with

{% static 'js/javascript.js' %}

For your reference:

usage: manage.py findstatic [-h] [--version] [-v {0,1,2,3}]
                            [--settings SETTINGS] [--pythonpath PYTHONPATH]
                            [--traceback] [--no-color] [--first]
                            staticfile [staticfile ...]

Finds the absolute paths for the given static file(s).

positional arguments:   staticfile


n.b., django's staticfiles app is not supposed to be used in production. In production, you are supposed to use a webserver to serve static files based. e.g., for nginx, you would include a block like this:

location /static {
    root /path/to/django;
}

To debug your static files check your STATIC_ROOT directory after you run collectstatic and make sure your files are there. If they are, make sure your webserver is configured to properly serve your static files directory.

这篇关于Django:连接路径位于基本路径组件之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆