user2308612
user2308612

Reputation: 37

Django won't serve some static files

I'm not sure what's happening but for some reasons django won't serve some scripts, and give me a not found error, despise the directory displayed being correct, while others in the same directory has no issue serving. Here is my file directory structure

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

Here are my setting:

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

the static files used in upload.html, out of which only lightbox.css and lightbox.js are served:

{% 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 %}

Here is my 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()

Here is what i get from running 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

Upvotes: 1

Views: 554

Answers (1)

Omid Reza Heidari
Omid Reza Heidari

Reputation: 680

place of static folder are wrong it should be at a same level of imagenes folder. like this

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

now you can put this at setting.py :

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

and in your template :

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

it should be work.

if prefer to visit THIS LINK to learn more about use static files

Upvotes: 1

Related Questions