Javascript is not working on Django

Asked

Viewed 337 times

0

I’ve tried every way, but I can’t make the JS work.

login js.:

$('.message a').click(function(){
   $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
});

login.html;

{% load static %}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="{% static 'gestao_clientes/css/login.css' %}" media="screen" />
    <script src='{% static "gestao_clientes/js/login.js" %}'></script>
</head>
<body>

    <div class="login-page">
      <div class="form">
        <form class="register-form">
          <input type="text" placeholder="name"/>
          <input type="password" placeholder="password"/>
          <input type="text" placeholder="email address"/>
          <button>create</button>
          <p class="message">Already registered? <a href="#">Sign In</a></p>
        </form>
        <form class="login-form">
          <input type="text" placeholder="username"/>
          <input type="password" placeholder="password"/>
          <button>login</button>
          <p class="message">Not registered? <a href="#">Create an account</a></p>
        </form>
      </div>
    </div>
</body>
</html>

I have tried: - with and without the type="text/javascript"

  • also: src="gestao_clientes/js/login.js"

My directory is as follows:

inserir a descrição da imagem aqui

The Settings part of my project is like this:

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: keep the secret key used in production secret!
SECRET_KEY = 'rwn)otj+b3_nt6u(0rw^n9bw^yhd-0!h!3xtx$bwsa1%7hdv(q'

# 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',
    'clientes',
    'login',
]

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 = 'gestao_clientes.urls'

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

STATICFILES_DIRS =[
    'estaticos',
]

MEDIA_URL = '/media/'

MEDIA_ROOT = 'media'

LOGIN_URL = ''

LOGIN_REDIRECT_URL = 'person_list'

The login and registration page will be the first page of the project, and I would like you to agree with this: https://codepen.io/colorlib/pen/rxddKy

But when I click *create an Account" it doesn’t open to enter the other data. Thanks for the help, I’m beginner in this part :)

  • I can’t see where you’re loading jQuery...

  • is it necessary? I don’t know about j'Query yet

  • You are using jQuery, you have to import in your login.html;

  • Download jQuery and put it in a folder in your app, and add the line <script src="{% Static 'main/vendor/jquery/jquery.min.js' %}"></script>

  • Okay, I’m gonna try!

1 answer

1


Explaining better, download jQuery and put it in the templates folder in your app and put it like this.

login.html

{% load static %}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="{% static 'gestao_clientes/css/login.css' %}" media="screen" />

    <script src="{% static 'gestao_clientes/login/templates/jquery/jquery-3.4.1.min.js' %}"></script>

    <script src='{% static "gestao_clientes/js/login.js" %}'></script>
</head>
<body>

    <div class="login-page">
      <div class="form">
        <form class="register-form">
          <input type="text" placeholder="name"/>
          <input type="password" placeholder="password"/>
          <input type="text" placeholder="email address"/>
          <button>create</button>
          <p class="message">Already registered? <a href="#">Sign In</a></p>
        </form>
        <form class="login-form">
          <input type="text" placeholder="username"/>
          <input type="password" placeholder="password"/>
          <button>login</button>
          <p class="message">Not registered? <a href="#">Create an account</a></p>
        </form>
      </div>
    </div>
</body>
</html>

login js.

$(document).ready(function(){
  $('.message a').click(function(){
     $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
  });
});
  • I did it but it didn’t work, it continued the same thing

  • Note that you have to put the correct path for your file, I put main/vendor/jquery/...file, you have to put for your case and for your correct board, I tested your code with jQuery and it worked.

  • In his test the Create an Account worked normal? I’ve changed the directory but it’s still not working

  • Sharing on github your project and I analyze

  • I’ve already edited the code so it works, I’m just trying to understand what’s wrong with importing your login.js or what’s wrong that it doesn’t work.

  • Now yes, you can download your login.js code, $(Document) was missing. ready(Function().

  • Thank you so much!! Thank you so much!! :)))

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.