簡體   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