【问题标题】:Add href in OnClick attribute in html form在 html 表单的 OnClick 属性中添加 href
【发布时间】:2022-02-01 09:00:39
【问题描述】:

我想在显示警报窗口后重定向到 python 烧瓶路由home

<input type="submit" value="Register" onclick="call(); log();"></input>

这是两个函数。

<script>

  function call()
  {
    window.alert("Congragulations! You Registerd Sucessfully ");
  }
  
</script>
<script>

  function log()
  {
    <a href="/home" > </a>
  }
  
</script>

【问题讨论】:

    标签: javascript python html flask


    【解决方案1】:

    如果我正确理解了您的项目,您希望在用户注册后转发该用户。同时,应该会出现一条消息,表明该过程已成功完成。

    我认为您的 javascript 方法不足以解决此问题,因为数据必须首先从客户端发送到服务器才能完成该过程。这里的输入也应该经过验证,并且可能会以某种方式处理或存储。

    我为你写了一个小例子,它接受表单数据并检查它。如果验证成功,将重定向用户并显示一条消息。为了转发和显示消息,我使用了flask包中的redirectflash

    Python 烧瓶

    import re
    from flask import Flask
    from flask import (
        flash,
        redirect,
        render_template,
        request,
        session,
        url_for
    )
    
    app = Flask(__name__)
    app.secret_key = b'your secret here'
    
    @app.route('/')
    def home():
        return render_template('index.html')
    
    @app.route('/register', methods=['GET', 'POST'])
    def register():
        if request.method == 'POST':
            username = request.form.get('username')
            if username and re.match(r'^[a-z0-9\_\-]+$', username, re.IGNORECASE):
                session['username'] = username
                flash('Congragulations! You Registerd Sucessfully.')
                return redirect(url_for('home'))
        return render_template('register.html')
    

    HTML (index.html)

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <nav>
          <ul style="list-style: none;">
            <li><a href="{{ url_for('register') }}">Register</a></li>
          </ul>
        </nav>
    
      {% with messages = get_flashed_messages() -%}
        {% if messages -%}
        <ul class="flashes">
          {% for message in messages -%}
          <li>{{ message }}</li>
          {% endfor -%}
        </ul>
        {% endif -%}
      {% endwith -%}
    
        <h1>Welcome {{ session.get('username', 'Guest') }}!</h1>
      </body>
    </html>
    

    HTML (register.html)

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Register</title>
      </head>
      <body>
        <form method="post">
          <div>
            <label for="username">Username</label>
            <input type="text" name="username" id="username" />
          </div>
          <input type="submit" value="Register" />
        </form>
      </body>
    </html>
    

    【讨论】:

    • 我想在按下注册按钮时显示“您注册成功”的消息,然后重定向到登录页面
    • @RehanAkhtar 在我看来,您只是部分理解了我的回答。由于也可以不使用页面将数据发送到端点,因此需要检查服务器上的条目。在客户端使用 javascript 进行双重检查是双重工作。省略服务器端验证是不安全的。我认为你应该在这方面重新考虑你的项目。
    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 2016-09-02
    • 2018-01-22
    • 2016-07-12
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多