Python django将静态路径添加到当前url
我的静态文件位于应用程序目录中的文件夹Python django将静态路径添加到当前url,python,django,django-views,django-staticfiles,Python,Django,Django Views,Django Staticfiles,我的静态文件位于应用程序目录中的文件夹assets。当我转到主页(/)时,静态文件从/assets/加载得非常好。如果我转到/house/,它会尝试从/house/assets/加载静态文件,这显然会导致文件无法加载,因为它们不在那里 这可能是设置的相关部分。py: ... import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) TEMPLATE_PATH = os.path.join(BASE_DIR, 'template
assets
。当我转到主页(/
)时,静态文件从/assets/
加载得非常好。如果我转到/house/
,它会尝试从/house/assets/
加载静态文件,这显然会导致文件无法加载,因为它们不在那里
这可能是设置的相关部分。py
:
...
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
STATIC_PATH = os.path.join(BASE_DIR, 'assets')
STATIC_URL = os.path.join(BASE_DIR,'/assets/')
STATICFILES_DIRS = (
STATIC_PATH,
)
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
...
我正在使用如下方式加载模板中的静态文件:
<link href="assets/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>
以下是意见:
def dashboard(request):
return render_to_response('index.html')
def house(request):
return render_to_response('house.html')
在过去的一个小时里,我一直在寻找解决方案,但没有成功。我发现问一个类似的问题,但没有帮助。任何帮助都将不胜感激。您应该将模板中的HTML标记从
<link href="assets/plugins/uniform/css/uniform.default.css"
rel="stylesheet" type="text/css"/>
到
请注意相对URL前面的斜杠
/
。如果没有它,浏览器将假定assets
目录是当前目录的子目录。有了它,它将始终从根目录开始。这确实修复了它。非常感谢。
<link href="assets/plugins/uniform/css/uniform.default.css"
rel="stylesheet" type="text/css"/>
<link href="/assets/plugins/uniform/css/uniform.default.css"
rel="stylesheet" type="text/css"/>