You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django表单中PyScript随机密码填充按钮触发表单提交问题求助

问题解决与代码修正

核心问题分析

  1. 按钮默认触发表单提交:<button>在表单内默认type="submit",点击会直接提交表单,跳过绑定的函数执行。
  2. PyScript变量作用域问题:mypass函数内的password是局部变量,全局的password不会被更新,导致后续无法获取生成的密码。
  3. PyScript代码执行时机错误:pyscript.write('python', password)写在函数外,只会执行一次初始空值,生成密码后不会更新显示内容。

修正后的完整代码

<body bgcolor="white">
    <py-script>
        import random
        # 全局变量存储生成的密码
        password = ''

        def mypass():
            global password  # 声明使用全局变量,确保生成的密码能被外部访问
            length = random.randint(8,16)
            list1 = []
            for i in range(length):
                list1.append(chr(random.randint(32,126)))
            password = ''.join(list1)
            pyscript.write('python', password)  # 生成密码后立即更新页面显示

        def fill_password():
            # 直接在PyScript中操作DOM,省去JS与PyScript的变量桥接
            from pyscript import document
            document.getElementById("password1").value = password
            # 自动同步到确认密码字段,优化用户体验
            document.getElementById("password2").value = password
    </py-script>

    <script>
        function myfun(){
            var a = document.getElementById("password1").value;
            var b = document.getElementById("password2").value;

            if(a!=b){
                document.getElementById("messages").innerHTML="Passwords do not match";
                return false;
            }
            return true; // 验证通过时返回true,允许表单正常提交
        }
    </script>

    <form onsubmit="return myfun();" action="register" method="post" >
        {% csrf_token %} 
        
        <p>
            <label for="user_name">User Name</label>
            <input type="text" name="user_name" id="user_name" placeholder="Antman56." autocomplete="on" >
        </p>
        <p>
            <label for="f_name">First Name</label>
            <input type="text" name="f_name" id="f_name" placeholder="Dave" autocomplete="on" >
        </p>
        <p>
            <label for="l_name">Last Name</label>
            <input type="text" name="l_name" id="l_name" placeholder="Lee" autocomplete="on" value="keyan" >
        </p>
        <p>
            <label for="email">Email ID</label>
            <input type="email" name="email" id="email" placeholder="davelee@gmail.com" autocomplete="on" >
        </p>
        <p>
            <label for="password1">Password</label>
            <input type="password" name="password1" id="password1" placeholder="123$aBc" >
        </p>
        <p>
            <label for="password2">Confirm Password</label>
            <input type="password" name="password2" id="password2" placeholder="123$aBc" ><span id="messages" style="color:red"></span>
        </p>
        <div id="python"></div>
        <!-- 添加type="button",避免触发表单提交 -->
        <button type="button" onclick="mypass()">create random password</button>
        <button type="button" onclick="fill_password()">fill the random password</button>
        <input type="submit" value="Submit" >
      
    </form>
</body>

关键修改点说明

  • 按钮类型修正:给两个功能按钮添加type="button",彻底避免点击时触发表单提交。
  • PyScript变量作用域:在mypass函数内用global password声明使用全局变量,确保生成的密码能被后续函数访问。
  • PyScript DOM操作优化:新增fill_password函数直接在PyScript中操作DOM,省去JS和PyScript的变量桥接,逻辑更简洁。
  • 表单验证完善:给myfun函数添加return true,确保密码匹配时允许表单正常提交。
  • 用户体验优化:填充密码时自动同步到确认密码字段,减少用户重复操作。

内容的提问来源于stack exchange,提问作者KarthiAKPS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 21:55:01