为什么每次我点击提交按钮时,我的网页和烧瓶一起创建会刷新

Why is my web page created along with flask getting refreshed every time I click the submit button

我正在创建一个 flask 程序,它根据用户的需求生成一定数量的密码。用户可以自由编辑密码。下面提到源代码。

Password_creator.py

from random import *
from itertools import *
import string
class password_generator():  
   def __init__(self,final_output,special_nproduction,final_specp): #creating the local variables
       self.final_output = final_output
       self.special_nproduction = special_nproduction
       self.final_specp = final_specp
      
      
   def alpha_rand_func(self): #for randomly selecting alphabets
       alpha = string.ascii_letters
       for i in range(4):
           self.final_output += choice(alpha)
        
   def symbol_rand_func(self): #for randomly selecting symbols
      symbol = string.punctuation
      for i in range(3):
         self.special_nproduction += choice(symbol)
             
   def int_rand_func(self): #for randomly selecting integers
      integer = string.digits
      for i in range(2):
         self.special_nproduction += choice(integer)
        
   def special_number_production(self): #for concatenating the integers and symbols
          self.special_nproduction = list(self.special_nproduction)
          init_perm = list(permutations((self.special_nproduction),5))
          selected_comb_init_list = choice(init_perm) #selecting a permutation of integers and symbols randomly
          mod_selected_comb_init = "" #for storing the permutation in the form of a variable
          for i in selected_comb_init_list: #iterating through the selected_comb_init_list
             mod_selected_comb_init += i 
          self.final_output += mod_selected_comb_init  # adding the integer-symbol combination to final output
   def final_output_production(self): #for concatenating all the characters, selecting a combination of it randommly and returning it as output
           return self.final_output #returns the final output
####################################          

####################################

exec_prog.py

from Password_creator import *
from flask import Flask,render_template,request,redirect,url_for
from table import Table
from flask_modus import Modus
app = Flask(__name__,template_folder='template')
modus= Modus(app)
@app.route("/")
def data_reveal(): # for user input for suggestions
    return render_template('input.html')

@app.route('/data-extract') # for extracting the user input
def data_extract():
    global sug
    sug = int(request.args.get('sug')) # to get the user input
    return redirect('/generate') # redirecting to generate
@app.route('/generate') # for generating the passwords
def generate():
    global final_g
    global data_x
    global password
    final_g = [] # for the passwords
    password = []
    for i in range(sug):
     v =  password_generator("","","") #for calling the class
     alpha_g = v.alpha_rand_func()    # line 25- 30; please refer the Password_creator.py file                 
     sym_g = v.symbol_rand_func()
     int_g = v.int_rand_func()
     spec_g = v.special_number_production()     
     final = v.final_output 
     final_g.append(v.final_output)
     passwd_obj = Table(v.final_output,'nil') # for storing them in a class 
     password.append(passwd_obj) # for storing them in a list
    print(final_g) 
    
    return render_template('output_disp.html',final_lst = password) # rendering the output_disp.html

###############
@app.route('/generate/<int:id>',methods = ["GET","PATCH","POST"]) # for modifying the function
def show(id):
     found  =  next(entry for entry in password if entry.id == id) # for finding the entry with the required entry
     if request.method  == b"PATCH": # for dealing with PATCH 
        found.passwd = request.form['edit_passwd']
        return redirect('/generate')
     return render_template('modify.html',found_entry = found)
    

@app.route('/generate/<int:id>/edit',methods = ["GET","PATCH","POST"]) # for showing the function
def modify(id):
    found  =  next(entry for entry in password if entry.id == id)
    return render_template('modify.html',found_entry = found)
###############

modify.html

{% extends 'str_main.html' %}
{% block content %}
<div id  = "modify-div">
<form id = "edit" action = "{{url_for('show',id  = found_entry.id)}}?_method=PATCH" method ="POST">
   <input type = "text" value = "{{found_entry.passwd}}" name = "edit_passwd">
   <input type ="submit" value = "save change" >
</form>
</div>
{% endblock %}

output_disp.html

{% extends 'str_main.html'%}
{% block content%}
<div id  =  "options">
  <div id  = "btn-output-1"><h6>Upload file</h6></div>
  <div id  = "btn-output-2"><h6>Main page</h6></div>
  <div id  = "btn-output-3"><h6>Save</h6></div> 
</div>
<div id = "output-head">
  <h1>Here are the suggestions</h1>
</div>
<div id  = "output-div">  
  <form id = "output-data" method = "POST">   
    <table>
         <tr>
           <th>Password</th>
           <th>Description</th>
         </tr>
         
         {% for i in final_lst %}
        <tr>    
           <td> <a href ="#">{{ i.passwd }}</a></td> 
           <td>{{ i.desc }}</td>
        </tr>     
         {% endfor %}
    </table>
    <input type = "text" value = "fileName" name = "fileName">
    <button type ="button" value= "saving file">Saving fhile</button>
  </form>
</div>   

{% endblock %}

table.py

class Table(): #class for storing the passwords with their description
    count  = 1
    def __init__(self,passwd,desc):
        self.passwd = passwd
        self.desc = desc
        self.id = Table.count
        Table.count += 1

这是 github 存储库 URL https://github.com/OCTRACORE/cs_Project

问题是,每当我转到 /generate/<int:id>/edit 路径并尝试编辑密码时,我都看不到 output_disp.html 中的变化。相反,页面被刷新并且我看到了一组新的建议。 那么我该怎么做才能解决这个问题。

好的,在查看了您的代码后,我想我已经解决了您的问题。

但是我想说你的代码中有很多不好的做法。其中最大的一个是在 Web 应用程序中使用全局变量。你不应该这样做。如果您计划 运行 此应用程序的多个实例来处理更多请求,那么其他实例将无法访问其他实例全局变量,因此您将遇到许多难以追踪的错误。

无论如何,我在您的 generate 函数中看到了这个问题。

当您提交对密码的更改时,它会被保存。但是那个保存被完全破坏了,因为在你的 generate 函数中你不检查以前的密码你也没有办法知道是否有什么改变或者你是否真的想要生成一组新密码。

我的建议

  • 了解网络应用程序的正确设置和结构。

  • 永远不要在 Web 应用程序中使用全局变量。太多的错误将由此而来,并且很难找到。

  • 您的应用看起来只是一个没有 javascript 或 ajax 的普通 Web 应用,因此不要对表单方法使用 PATCH 或 DELETE。使用 GET 和 POST 并使用不同的 url 来决定如何处理这些请求。

如果 Flask 没有关于如何为您的应用设置 endpoints/urls 的好教程,请查看 Django 项目的设置。 Flask 在很多方面模仿 Django 如何处理 endpoints/urls.