繁体   English   中英

无法使用 Flask 在 HTML 中加载 Javascript 文件

[英]Can't load Javascript file in HTML using Flask

我正在尝试将 js 文件导入 my.html 文件并在其中使用 function 但它似乎不起作用: script.js

function HidePassword() {
    var x = document.getElementById("passwordInput");
    if (x.type === "password") {
      x.type = "text";
    } else {
      x.type = "password";
    }
  }

newclient.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title> Onboarding Portal - New Client</title>
  </head>
  <script type="text/javascript" src="../script.js"></script>
  <body>
    <a href="/" target="_self">
      <button>Back</button>
    </a>
    <center>
      Onboarding Portal
      <br /><br /><br />

      <form method="POST" action="/">
        Password:
        <input
          type="password"
          name="password"
          value=""
          id="passwordInput"
          required
        /><br /><br />
        <input type="checkbox" onclick="HidePassword()" />Show Password
    </center>
  </body>
</html>

我在这里错过了什么吗? 我不应该像这样使用script.js吗?

编辑:这是结构:

在此处输入图像描述

在此处输入图像描述

Uncaught ReferenceError: HidePassword is not defined at HTMLInputElement.onclick

from flask import Flask, render_template, request
import onboardingscript
from onboardingscript import onboard_client, add_rec_to_existing_client
import json
from configparser import ConfigParser
import pandas as pd
from onboarding2 import onboard_client2




config_object = ConfigParser()
config_object.read("config.ini")

# create an instance of flask
app = Flask(__name__)

# Rename to the correct snake notation
# instantiate the app in one function -- COMPLETE
# Start adding config files to replace the credentials and other sensitive information
# add more error checking to your calls
# compartmentalize your functions 

def create_app():
    # # print(df)
    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/new-client')
    def new_client():
        return render_template('newclient.html')

    @app.route('/existing-client')
    def existing_client():
        return render_template('existingclient.html')

    @app.route('/', methods=['POST'])
    def onboard_new_client():
        # throw all variables captured by form and throw it into script, and run it
        #create a large function that takes in the calls and have it run in sequential order for this
        onboarding_data = {
            'clientFirstName': request.form['clientFirstName'],
            'clientLastName': request.form['clientLastName'],
            'clientId': request.form['clientId'],
            'clientEmail': request.form['clientEmail'],
            'envOption': request.form['env']
        }
        result = onboard_client2(onboarding_data)
        # result = onboard_client(onboarding_data)
        if (result == True):
            return render_template('confirmation.html')
        else:
            return render_template('failure.html')

    @app.route('/a', methods=['POST'])
    def onboard_existing_client():
        onboarding_data = {
            'clientEmail': request.form['clientEmail'],
            'envOption': request.form['env']
        }
        result = add_rec_to_existing_client(onboarding_data)
        if (result == True):
            return render_template('confirmation.html')
        else:
            return render_template('failure.html')
    return app

if __name__ == '__main__':
    app = create_app()
    app.run(debug=True)

编辑:我正在考虑在<script>块中手动输入它。 这只是我试图通过将它放在 one.js 文件中来节省空间/时间,因为这个 function 在两个地方使用。

templates的同一级别创建一个名为static的目录,并将您的脚本文件放入其中。 您将能够像这样加载它:

<script type="text/javascript" src="/static/script.js"></script>

请注意,这也适用于其他文件,例如图像或 css 文件。

注意: <script>导入的当前 position 不正确。 要么把它放在你的<head>里面,要么放在<body>里面。 即使它会在大多数现代浏览器上加载脚本文件,这也不是有效的 HTML 语法,并且会在 W3C 验证器中出现错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM