0

I have a problem with include a js script file to Django projects. Script works with tags in html file.

Here is my 1st try:

<!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/style.css"  type="text/css" > -->
    <!-- jquery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <link rel="stylesheet" href="/static/css/main.css">
    <script type="text/javascript" src="/static/js/script.js"></script>
</head>

The 2nd try:

{% load static %}
<!doctype html>
<html lang="en">
  <head>
    {% load static %}
    <!-- 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/style.css"  type="text/css" > -->
    <!-- jquery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <link rel="stylesheet" href="/static/css/main.css">
    <script src="{% static 'js/script.js' %}"></script>
</head>

The both tries do not work. Do you have any ideas. Path to js file is static/js/script.js.

settings.py:

from pathlib import Path


BASE_DIR = Path(__file__).resolve().parent.parent
print("BASE_DIR", BASE_DIR)

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',
]

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',
]

ROOT_URLCONF = 'eeg_app.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ 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 = 'eeg_app.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

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',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
STATIC_ROOT = BASE_DIR / 'staticfiles'

urls.py:

from django.contrib import admin
from django.urls import path
from eeg_app.views import index
from django.conf import settings

**urlpatterns** = [
    path('admin/', admin.site.urls),
    path('', index)
]

enter image description here

Css and image from static are included without issues. Thanks for your answers.

2
  • Add your project url.py to question Commented Jan 20, 2021 at 22:09
  • Already added to project Commented Jan 20, 2021 at 22:28

1 Answer 1

1

Serving static files during development:

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Sign up to request clarification or add additional context in comments.

5 Comments

Thats still not work if I had to do something like that: urlpatterns = [ path('admin/', admin.site.urls), path('', index) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
@Oskar, Are static files loaded when you open localhost:8000/admin? Share a screenshot of your project directory.
Screnshot added
@Oskar, What you see if you run manage.py collectstatic?
@Oskar, And what happens if you try to access http://localhost:8080/static/js/script.js? See you Page not found (404) 'js/script.js' could not be found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.