Home > database >  Jquery not loading from static files
Jquery not loading from static files

Time:06-03

This is my project structure:

pack
├── mysite
│   ├── blog
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── __pycache__
│   │   ├── tests.py
│   │   └── views.py
│   ├── db.sqlite3
│   ├── manage.py
│   ├── mssql_app
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms_old.py
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   ├── models.py
│   │   ├── models.txt
│   │   ├── __pycache__
│   │   ├── routers.py
│   │   ├── templates
│   │   ├── tests.py
│   │   ├── urls.py
│   │   ├── views_old.py
│   │   └── views.py
│   ├── mysite
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── static
│       ├── admin
│       └── js
             └── jquery-3.1.1.min.js
└── venv

In settings.py there is:

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'mysite/static')
]

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

I'm trying to load jquery in ~/pack/mysite/mssql_app/templates/base.html as follows:

{% load static %}<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
  <script src="{% static 'js/jquery-3.1.1.min.js' %}"></script>
 
  
  <title>{% block title %}Packing{% endblock %}</title>
</head>

But python manage.py runserver writes:

"GET /static/js/jquery-3.1.1.min.js HTTP/1.1" 404 1690

Here is ~/pack/mysite/mysite/urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'', include('mssql_app.urls')),
]

and ~/pack/mysite/mssql_app/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.show, name ='show'),
    url(r'create', views.create, name ='create'),
    url(r'save_pw', views.save_pw, name ='save_pw'),
]

How can I fix it?

The solution from @Lakshyaraj Dash helped, but jquery doesn't work when deploying the project with apache2. Isn't there some universal settings which work after deployment as well? Or what are the steps to do (regarding to static files) when deploying the app?

CodePudding user response:

In the STATICFILES_DIRS write down

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static/')
]

Keep either one configuration STATICFILES_DIRS or STATIC_ROOT.

Static root can be used only when the debug is false.

  • Related