Static files not passed to the browser

Hello I’m trying to load my static files to my templates but they’re not getting passed to my browser. When I look at the developer tools, I don’t see my Javascript and HTML files being passed to the browser. I have my static folder in a separate directory not inside any apps. I also wrote the required code in the settings files and in the HTML templates. I have my code and direct displayed below.

I also heard that perhaps this issue is due to my cache and the solution is to disable cache in the development network but I tried that and it still doesn’t work. I have Django version 4.2.17. And finally I tried including Javascript and CSS in separate project without Django and they work but I can’t add them with Django.

Project Directory

Settings file

1 Like

HTML Template

1 Like

Browser not receiving Javascript (index.js) and CSS (styles.css) files

Welcome @vincentlam-coder !

Side note: For future reference, please do not post images of code, templates, error messages, or any other text information.
Copy/paste the text into the body of your post, surrounded between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template, HTML, error message, etc), then another line of ```.

Is this situation occuring in a development environment (running using runserver) or a production environment (running using something like gunicorn or uwsgi?

What is the actual HTML being rendered for that reference? (What shows up in the HTML in the page?)

Do you see the requests being made from the browser to the server?

If a development environment, what is your INSTALLED_APPS setting?

If this is referring to a production environment, how are you running your project? How is your file server configured for the static files?

It’s in the development server, and I have all my apps installed. In the browser, when enter the page I in the terminal GET /login/ HTTP/1.1 but it’s not requesting the static files. For the HTML templates, It’s just a basic login page with a form provided by the django.contrib.auth app.

Also I’m on a MAC if that’s important

What is your INSTALLED_APPS setting?

What is the HTML that was rendered for those elements and sent to the browser?

And since it’s a development setting, also provide your root urls.py contents.

It looks like your application code is in the cap_app directory. So your static directory should be beneath that directory for this to work. Also, the STATICFILES_FINDERS setting is redundant, since you’re just redefining the default setting. Here’s my static files settings that I copy from project to project - the comments help me remember what each one does. :slight_smile:

# The path that the static files will be served from
STATIC_URL = "/static/"
# The directory that collectstatic will collect static files to
STATIC_ROOT = BASE_DIR / "staticfiles"
# The directory that static files will be collected from
STATICFILES_DIRS = [
    BASE_DIR / "static",
]

But I think just moving the static folder to the cap_app folder will fix your issue.

1 Like

It does not need to be. It is perfectly valid for the static directory to be directly in the BASE_DIR directory - you even have it in your configuration where you explicitly define it as a directory to search.

And, if he’s not even seeing the requests on the server returning 404s, then the issue is definitely not in the location of the static files, because the browser isn’t trying to request them - regardless of their location.

That’s why more detailed information is necessary here to identify the root cause.

1 Like

INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cap_app',
    'main',
]

HTML Template
for the CSS I just tried to colour the body background.
for the Javascript, I tried to make it call a function called test defined in the index.js file when the button is clicked

{% extends 'base.html' %}
{% load static %}

<!DOCTYPE html>

<html>

<head>
    {% block head %}
        <title>Login Page</title>
    {% endblock %}
    <link rel="stylesheet" href="{% static 'styles.css' %}">
</head>

<body>
    <script src="{% static 'index.js' %}"></script>
    {% block top %}
        <div class = "col-2 p-4">
            <a href="/register" class="btn btn-primary"> Register </a>
        </div>
        <div class = "col-2 p-4">
            <a href="/password_reset" class="btn btn-primary"> Reset Password </a>
        </div>
    {% endblock%}

    {% block body %}
        <div class = "container mx-auto" style = "width: 575px">
            <h1>Welcome To The Community </h1>
            <h2>Login Here</h2>

            <form method = "POST">
                {% csrf_token %}
                {{form.as_p}}
                <button type = "submit" class="btn btn-primary">Login</button>
            </form>
            <br>
            <button id = "button" onclick="test()">button</button>
        <div>
    {% endblock %}

</body>

</html> 

Project urls.py
The first 5 are admin and for resetting password using the django.contrib.auth
next 2 are for my main and cap_app apps
last one is also using django.contrib.auth which is used to login and logout
The urlspatterns line on the bottom was for serving MEDIA images/files from user input form

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as v
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('password_reset/',v.PasswordResetView.as_view(template_name = 'registration/password_reset.html'), name = 'password_reset'),
    path('password_reset/done/',v.PasswordResetDoneView.as_view(template_name = 'registration/pw_reset_done.html'), name = 'password_reset_done'),
    path('reset/<uidb64>/<token>/',v.PasswordResetConfirmView.as_view(template_name = 'registration/pw_confirm.html'), name = 'password_reset_confirm'),
    path('reset/done/',v.PasswordResetCompleteView.as_view(template_name = 'registration/pw_complete.html'), name = 'password_reset_complete'),
    path('', include('cap_app.urls')),
    path('', include('main.urls')),
    path('', include('django.contrib.auth.urls')),
]

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

Also I forgot to mention that when I enter in the URL domain/static/styles.css or domain/static/index.js, I get sent to the file and no issue occurs. It’s just for some reason browser doesn’t make a request for the static folders

So if the browser is the potential issue, I’m currently using safari if that’s important

I do see a problem here - the references to those files aren’t in a {% block element, so they’re not going to get rendered in the final page. All HTML in a template that is extending a parent template must be in a block - otherwise, the rendering engine doesn’t know where to put that HTML in the page.
So none of the HTML outside the blocks are being sent to the browser.

Also, as a general rule, having multiple identical path entries including other url files is, a really bad idea:

To avoid odd and difficult-to-diagnose issues, you’re a lot better off assigning different root paths for each of these. There is no benefit for doing it this way, and a lot of possibilities of problems.

OH MY GOD YOU’RE RIGHT. I added the script and link tag inside the blocks and it worked. THANK YOU SO MUCH. I really appreciated this since I’m trying to work on this site to add to my resume. Once again thank you

You’re right - there was something about the folder structure that didn’t look right. I thought the BASE_DIR might be the cap_app directory, but that doesn’t seem right now that I look at it again. And even if I was right, that wouldn’t explain why there were no 404 errors in the network tools. A template issue was a much more likely explanation, which is what ended up being the issue. Thanks.