django 2.0.3和浏览器未显示静态文件 [英] django 2.0.3 and Browser not showing static files
问题描述
django 2.0.3和Python 3.6.x FireFox,Chrome,Safari
django 2.0.3 and Python 3.6.x FireFox, Chrome, Safari
我不确定这是django问题,因为这是我已经解决的问题从未发生过。我希望有人以前见过这个,或者知道我做错了。
I'm not sure that this is a django issue as this is something I've never seen happen. I am hoping someone has seen this before or knows what I am doing wrong.
我的浏览器无法通过runserver找到我的静态文件。奇怪的是模板中的URL正确,如果我将URL粘贴到浏览器中,则会找到文件。
My browser will not find my static files with runserver. What is strange is that the urls within the templates are correct and if I paste the urls into the browser, the files are found.
如果我运行
./ manage.py findstatic --verbosity 2 CSS /style.css
已找到文件。但是浏览器Firefox,Chrome,Safari(所有最新版本)都在我访问静态资源的页面的Web控制台中显示404。
The files are found. Yet the browser, Firefox, Chrome, Safari (all latest versions) both show a 404 in the WebConsoles of the pages where I access the static resources.
< link rel = stylesheet href = {%static'trans / css / style.css?1510690221335'%}>
不起作用。如果我更改为实际URL
Does not work. If I change to the actual URL
< link rel = stylesheet href = http:// localhost:8080 / trans / css /style.css?1510690221335\">
一切正常。
我相关的配置(据我所知)是
My relevant (As best I can tell) configuration is
STATIC_ROOT = str(ROOT_DIR('staticfiles'))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
str(APPS_DIR.path('static')),
]
/#staticfiles-finders
/#staticfiles-finders
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
我还添加了
static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
到我的urls.py
to my urls.py
所有权限在我的 static
文件夹和子文件夹中是正确的。有人看过这个并且知道解决方法吗?谢谢。
All permissions on my static
folder and sub folders are correct. Has anyone seen this before and know of a resolution? Thank you.
推荐答案
当我将静态文件放在与我的应用程序不对应的文件夹中时,我遇到了同样的问题请按照以下步骤操作
I had same issue, when i place my static files in a folder that doesn't correspond to my app name follow the step below
- 确保django.contrib.staticfiles包含在INSTALLED_APPS中。
-
在设置文件中定义STATIC_URL,例如:
- Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL ='/ static /'
STATIC_URL = '/static/'
在您的模板中,使用静态模板标记通过配置的STATICFILES_STORAGE为给定的相对路径构建URL。
In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.
{%load static%}
{% load static %}
src = {%static my_app / example.jpg%}
src="{% static "my_app/example.jpg" %}"
存储您的静态文件位于应用程序中名为static的文件夹中。例如my_app / static / my_app / example.jpg
Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg
查看完整文档此处
这篇关于django 2.0.3和浏览器未显示静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!