# Static files not passed to the browser

**URL:** <https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559>\
**Category:** Mystery Errors\
**Created:** [December 30, 2024, 1:43am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559 "2024-12-30T01:43:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 1:43am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/1 "2024-12-30T01:43:12Z")

</div>

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

 ![Screenshot 2024-12-29 at 8.35.29 PM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/f/5f1d968d764b8c84236c82c6a0a57272f7e96744.png)

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 1:44am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/2 "2024-12-30T01:44:16Z")

</div>

Settings file

 ![Screenshot 2024-12-29 at 8.37.34 PM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/9/a/9ae303c2a798fd888e84b99f0e4be6197a37d12d.png)

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 1:46am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/3 "2024-12-30T01:46:25Z")

</div>

HTML Template

 ![Screenshot 2024-12-29 at 8.38.25 PM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/8/2/82201b9560b21818d27a560aa4a13661e9174dfd.png)

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 1:47am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/4 "2024-12-30T01:47:06Z")

</div>

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

 ![Screenshot 2024-12-29 at 8.40.56 PM](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/c/5c8ac90c00ea324c91ce724c43fbba33757bc9a8.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2024, 1:51am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/5 "2024-12-30T01:51:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 1:55am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/6 "2024-12-30T01:55:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2024, 2:09am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/7 "2024-12-30T02:09:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stuartmaxwell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stuartmaxwell/32/1135_2.png) [@stuartmaxwell](https://forum.djangoproject.com/u/stuartmaxwell)\
**Post date:** [December 30, 2024, 10:03am UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/8 "2024-12-30T10:03:16Z")

</div>

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. 🙂

```python
# 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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2024, 1:00pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/9 "2024-12-30T13:00:24Z")

</div>

> [@stuartmaxwell](#):
>
> So your `static` directory should be beneath that directory for this to work.

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.

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 3:54pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/10 "2024-12-30T15:54:26Z")

</div>

INSTALLED\_APPS

```auto
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

```auto
{% 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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 4:09pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/11 "2024-12-30T16:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 4:26pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/12 "2024-12-30T16:26:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2024, 5:04pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/13 "2024-12-30T17:04:23Z")

</div>

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:

> [@vincentlam-coder](#):
>
> ```auto
> path('', include('cap_app.urls')),
> path('', include('main.urls')),
> path('', include('django.contrib.auth.urls')),
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![vincentlam-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vincentlam-coder/32/25985_2.png) [@vincentlam-coder](https://forum.djangoproject.com/u/vincentlam-coder)\
**Post date:** [December 30, 2024, 5:13pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/14 "2024-12-30T17:13:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stuartmaxwell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stuartmaxwell/32/1135_2.png) [@stuartmaxwell](https://forum.djangoproject.com/u/stuartmaxwell)\
**Post date:** [December 30, 2024, 8:33pm UTC](https://forum.djangoproject.com/t/static-files-not-passed-to-the-browser/37559/15 "2024-12-30T20:33:03Z")

</div>

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.
