Home > Back-end >  Unable to load JavaScript and images in Django
Unable to load JavaScript and images in Django

Time:02-04

Could not able to load any javascript files and images Source in Browser instead of using global... also tried to use app level static but nothing changes

setting.py and directory

Terminal

index.html

<!-- external javascripts -->
<script scr="{% static 'js/jquery.js' %}"></script>
<script scr="{% static 'js/jquery-ui.min.js' %}"></script>
<script scr="{% static 'js/bootstrap.min.js' %}"></script> -->
<!-- JS | jquery plugin collection for this theme -->
<script scr="{% static 'js/jquery-plugin-collection.js' %}"></script>
<!-- Revolution Slider 5.x SCRIPTS -->
<script scr="{% static 'js/revolution-slider/js/jquery.themepunch.tools.min.js' %}"></script>
<script scr="{% static 'js/revolution-slider/js/jquery.themepunch.revolution.min.js' %}"></script>

urls.py

from django.contrib import admin
from django.urls import path,include
from django.conf.urls.static import static
from . import settings
from user.views import home_view
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',home_view, name='home'),
    path('faculty/', include('faculty.urls')),
    path('user/', include('user.urls'))

]  static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

all the static files are collected at assets folder, assets and static both contain same files assets folder

static folder

CodePudding user response:

Create a folder called assets and put the static files in it

And put these settings of static and media files in the settings.py file and comment the previous settings :

STATIC_URL = 'static/'

STATICFILES_DIRS = [
    BASE_DIR / "assets",
]
MEDIA_URL = 'media/'
MEDIA_ROOT = 'media/'

Put this piece of code in the urls.py file :

from django.conf.urls.static import static
from django.conf import settings

urlpatterns  = urlpatterns   static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


urlpatterns  = urlpatterns   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

CodePudding user response:

In your Terminal, it looks like it is able to successfully get to most of the static files we can see (code 200), and there are only a few unavailable resources (code 404). We can't really see anything about errors loading js and image files in your screenshot of the Source in Browser.

If some files are available and some new ones are not, one possibility is you may need to run django's 'collectstatic' command, to collect all static files into the folder where they will be served from - like: python manage.py collectstatic https://docs.djangoproject.com/en/4.1/ref/contrib/staticfiles/#collectstatic

  • Related