gpt4 book ai didi

python - Django 不会提供一些静态文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:43 24 4
gpt4 key购买 nike

我不确定发生了什么,但由于某些原因,django 不会提供某些脚本,并给我一个未找到的错误,鄙视显示的目录是正确的,而同一目录中的其他目录则没有问题。这是我的文件目录结构

mysite/
├── imagenes
│   ├── migrations
│   │   └── __pycache__
│   ├── __pycache__
│   ├── recibo
│   ├── static
│   │   ├── css
│   │   └── js
│   └── templates
│   └── imagenes
├── media
│ └── recibos
│   └── 2017
│   └── 03
│   └── 10
├── mysite
│   ├── __pycache__
│   └── static
│   └── sitioI
├── static
│   ├── css
│   └── js
│   └── jquery-file-upload
└── templates
└── registration

这是我的设置:

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIR= [
os.path.join(BASE_DIR, "static"),
]

upload.html中使用的静态文件,其中仅提供lightbox.css和lightbox.js:

{% extends 'imagenes/base.html' %}
{% load static %}
{% block style %}
<link rel="stlyesheet" href="{% static 'css/lightbox.css' %}">
{% endblock %}
.
.
.
{% block scripts %}
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script src="{% static 'js/lightbox.js' %}" ></script>
<script src="{% static 'js/jquery-file-upload/vendor/jquery.ui.widget.js' %}"></script>
<script src="{% static 'js/jquery-file-upload/jquery.iframe-transport.js' %}"></script>
<script src="{% static 'js/jquery-file-upload/jquery.fileupload.js' %}"></script>
<script src="{% static 'js/upload.js' %}"></script>
{% endblock %}

这是我的 urls.py:

urlpatterns = [
url(r'^imagenes/', include('imagenes.urls')),
url(r'^admin/', admin.site.urls),
url(r'^cuentas/perfil/$', editar_usuario, name='perfil'),
url(r'^cuentas/registro/$', RegistrationView.as_view(form_class=RegistroUsuario), name='registro'),
url(r'^cuentas/', include('registration.backends.hmac.urls')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

这是我从运行 runserver 中得到的结果

[13/Mar/2017 22:41:52] "GET /static/js/jquery-file-upload/jquery.fileupload.js HTTP/1.1" 404 1853
[13/Mar/2017 22:41:52] "GET /static/js/jquery-file-upload/jquery.iframe-transport.js HTTP/1.1" 404 1871
[13/Mar/2017 22:41:52] "GET /static/js/jquery-file-upload/vendor/jquery.ui.widget.js HTTP/1.1" 404 1871
[13/Mar/2017 22:41:52] "GET /static/js/upload.js HTTP/1.1" 404 1763
[13/Mar/2017 22:41:53] "GET /static/js/jquery-file-upload/vendor/jquery.ui.widget.js HTTP/1.1" 404 1871
[13/Mar/2017 22:41:53] "GET /static/js/jquery-file-upload/jquery.iframe-transport.js HTTP/1.1" 404 1871
[13/Mar/2017 22:41:53] "GET /static/js/jquery-file-upload/jquery.fileupload.js HTTP/1.1" 404 1853
[13/Mar/2017 22:41:53] "GET /static/js/upload.js HTTP/1.1" 404 1763

最佳答案

静态文件夹的位置错误,它应该与imagenes文件夹处于同一级别。像这样

my site/
imagenes/
setting.py
imaagenes2/ #beacuse i dnt khow this floder name in your project
views.py
static/
css/
main.css
js/
main.js

现在您可以将其放在 setting.py 中:

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

并在您的模板中:

{% load staticfiles %}
<link rel="stylesheet" src="{% static "css/main.css" %}"/>

应该可以工作。

如果愿意访问 THIS LINK了解有关使用静态文件的更多信息

关于python - Django 不会提供一些静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com