How do you run Javascript in Django?

Paul

This is my first time doing anything like this and google has not helped me much. I can get Javascript to work as part of the html but not as its own file.

I've set up my static files and they work for my CSS but I cannot run any Javascript from the static folder.

The path is below:

enter image description here

This is in my "base.html"

<head>
  <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="/static/css/base.css"> <!-- Works -->
  <!-- Custom CSS -->
    <link rel="stylesheet" href="/static/css/darkmode.css"> <!-- Works -->
  <!-- Utilities CSS-->
  <link rel="stylesheet" href="/static/css/star_rate.css"> <!-- Works -->

</head>

<!-- Scrips -->
<script src="/static/js/all_pages.js" type="text/javascript"></script> <!-- Doesn't Work-->
<script src="/static/js/jquery-3.5.1.min.js" type="text/javascript"></script> <!-- Doesn't Work-->

This is "all_pages.js"

alert("Test 2");

and this is an extension of base called "test.html"

{% extends 'base.html' %}
{% block content %}

{% load static %} 
<script src="/static/js/all_pages.js" type="text/javascript"></script> <!-- I added this in hopes it'd fix the issue -->

<!-- VVV I added this to see if js was not working at all  -->
<script type="text/javascript">
alert('Test 01');
</script>
<!-- ^^^ I added this to see if js was not working at all  -->

{% endblock %}

I only get the alert "Test 01"

I would expect to get "Test 01" and "Test 02"

Edit:

Full base.html

{% load static %}

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="/static/css/base.css"> <!-- Works -->
  <!-- Custom CSS -->
    <link rel="stylesheet" href="/static/css/darkmode.css"> <!-- Works -->
    <!-- <link rel="stylesheet" href="/static/css/w95.css"> -->
  <!-- Utilities CSS-->
  <link rel="stylesheet" href="/static/css/star_rate.css"> <!-- Works -->

</head>

<!-- Scrips -->
<script src="/static/js/all_pages.js" type="text/javascript"></script> <!-- Doesn't Work -->
<script src="/static/js/jquery-3.5.1.min.js" type="text/javascript"></script> <!-- Doesn't Work -->

<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand" href="/">Not Velox</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarText">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item">
              <a class="nav-link" href="create/">Create Franchise</a>
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="#">Test cant push<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Test 2</a>
            </li>
          </ul>
          <!-- Username -->
          <span class="navbar-text">
            {% if user.is_authenticated %}
            <a class="navbar-brand" href="#"> <!-- Need to add link to user Profile -->
                <button class="btn btn-outline-secondary" type="button">{{user.username}}</button>
            </a>
            {% endif %}
          </span>
          <!-- Log out -->
          <span class="navbar-text">
            {% if user.is_authenticated %}
            <a class="navbar-brand" href="#">
                <form action="/signup/logout/" method="POST">
                    {% csrf_token %}
                    <button class="btn btn-outline-secondary" type="submit">
                        <svg class="bi bi-box-arrow-in-left" width="1em" height="1em" viewBox="0 0 16 16"
                            fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                d="M7.854 11.354a.5.5 0 0 0 0-.708L5.207 8l2.647-2.646a.5.5 0 1 0-.708-.708l-3 3a.5.5 0 0 0 0 .708l3 3a.5.5 0 0 0 .708 0z" />
                            <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5h-9a.5.5 0 0 0 0 1h9A.5.5 0 0 0 15 8z" />
                            <path fill-rule="evenodd"
                                d="M2.5 14.5A1.5 1.5 0 0 1 1 13V3a1.5 1.5 0 0 1 1.5-1.5h8A1.5 1.5 0 0 1 12 3v1.5a.5.5 0 0 1-1 0V3a.5.5 0 0 0-.5-.5h-8A.5.5 0 0 0 2 3v10a.5.5 0 0 0 .5.5h8a.5.5 0 0 0 .5-.5v-1.5a.5.5 0 0 1 1 0V13a1.5 1.5 0 0 1-1.5 1.5h-8z" />
                        </svg>
                        logout
                    </button>
                </form>
            </a>
            {% endif %}            
          </span>
          <!-- Sign up -->
          <span class="navbar-text">
            {% if not user.is_authenticated %}
            <a class="navbar-brand" href="/signup/">
                <button class="btn btn-outline-secondary" type="button">
                    <svg class="bi bi-person-plus-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
                        xmlns="http://www.w3.org/2000/svg">
                        <path fill-rule="evenodd"
                            d="M1 14s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H1zm5-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm7.5-3a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1H13V5.5a.5.5 0 0 1 .5-.5z" />
                        <path fill-rule="evenodd" d="M13 7.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1H14v1.5a.5.5 0 0 1-1 0v-2z" />
                    </svg>
                Sign Up
                </button>
            </a>
            {% endif %}
          </span>
          <!-- Log in -->
          <span class="navbar-text">
            {% if not user.is_authenticated %}
            <a class="navbar-brand" href="/signup/login">
                <button class="btn btn-outline-secondary" type="button">
                    <svg class="bi bi-box-arrow-in-right" width="1em" height="1em" viewBox="0 0 16 16"
                        fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                        <path fill-rule="evenodd"
                            d="M8.146 11.354a.5.5 0 0 1 0-.708L10.793 8 8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0z" />
                        <path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9A.5.5 0 0 1 1 8z" />
                        <path fill-rule="evenodd"
                            d="M13.5 14.5A1.5 1.5 0 0 0 15 13V3a1.5 1.5 0 0 0-1.5-1.5h-8A1.5 1.5 0 0 0 4 3v1.5a.5.5 0 0 0 1 0V3a.5.5 0 0 1 .5-.5h8a.5.5 0 0 1 .5.5v10a.5.5 0 0 1-.5.5h-8A.5.5 0 0 1 5 13v-1.5a.5.5 0 0 0-1 0V13a1.5 1.5 0 0 0 1.5 1.5h8z" />
                    </svg>
                    Login
                </button>
            </a>
            {% endif %}
          </span>
        </div>
      </nav>
    <div class="p-3 m-3">
        {% block content %}
        {% endblock %}
    </div>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
        crossorigin="anonymous"></script>
</body>

</html>

settings.py

"""
Django settings for pyshop project.

Generated by 'django-admin startproject' using Django 3.0.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'products.apps.ProductsConfig',
    'franchise.apps.FranchiseConfig',
    'accounts.apps.AccountsConfig',
    'debug_toolbar'
   
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

INTERNAL_IPS = ("http://127.0.0.1/")

ROOT_URLCONF = 'pyshop.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'pyshop.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'root')

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

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR,'media')


Ceetified_karma

try adding this ur settings

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
   )
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related