0

这部分负责将链接发送给用户。到目前为止,它完美地完成了这项工作,我总是收到电子邮件

app = Flask(__name__)
app.secret_key = 'b11223344AaadD$$r.,IIr]]tP[tu@urr'
app.config.from_pyfile('config.cfg')

mail = Mail(app)

s = URLSafeTimedSerializer(app.config['SECRET_KEY'])

engine = create_engine("postgresql://postgres:andersen23@localhost:5432/test")
db = scoped_session(sessionmaker(bind=engine))

@app.route('/recover', methods=['POST', 'GET'])
def recover():
   headline = 'Recover Your Password'
   alert = 'Type Your Email'
   alert_ = ''
   if request.method == 'GET':
      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

   email = request.form.get('email')
   session['email'] = email

   mail1 = db.execute("SELECT contact0.email FROM person JOIN contact0 ON contact0.id = person.id 
   WHERE email = :email",
   {"email": email}).fetchone()

   token = s.dumps(email, salt='confirm')
   link = url_for('confirm', token=token, _external=True)

   msg = Message('Confirm Email', sender='esdavitnem@gmail.com', recipients=[email])

   name = db.execute(
   "SELECT person.first_name FROM person JOIN contact0 ON contact0.id = person.id WHERE email = 
   :username",
   {"username": email}).fetchone()


   if not isinstance(mail1, type(None)):
      alert_ = f"Link Sent to {email}! \n Expires in 5 minutes!"
      msg.body = f"Dear {name[0]}, \n\nYour link is {link} \n\nBest,\nDavid from Drunkify"
      mail.send(msg)

      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

   if isinstance(mail1, type(None)):
      alert_ = f"No user exists with {email} email"
      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

这部分负责打开令牌并向用户显示 html 代码。

我的主要问题是 render_template 仅适用于我的模板中的一个 html 文件。

register1.html是我的用户更改密码的页面。但是代码错误消息不断建议我将它们重定向到 register.html

@app.route('/confirm_email/<token>')
def confirm(token):
   headline = 'Type Your New Password'
   try:
      email = s.loads(token, salt='confirm', max_age=300)
   
   except SignatureExpired:
     return 'The Token Expired'
   return render_template('register1.html', headline=headline)

Register1.html

<!DOCTYPE html>
<html lang="en">

<head>
<!-- Title Page-->
<title>Recover Your Password</title>

<link href="../static/main.css" rel="stylesheet" media="all">
</head>

<body>
<div class="page-wrapper bg-gra-01 p-t-180 p-b-100 font-poppins">
<div class="wrapper wrapper--w780">
    <div class="card card-3">
        <div class="card-heading"></div>
        <div class="card-body">
            <h2 class="title">Change Your Password</h2>
            <form action = "{{ url_for('confirm') }}" method="post">
                <div class="form-group">
                     <input class="form-control" type="password" 
   
 placeholder="New Password" name="password" required>
                    <div class="help-block with-errors"></div>
                </div>

                <div class="p-t-10">
                    <button type="submit" class="btn btn--pill btn--green">
                        Submit
                    </button>
                </div>
            </form>
        </div>
    </div>
    </div>
 </div>

</body>

</html>
4

1 回答 1

0

您所看到的是期望之间的不匹配

def confirm(token):

它需要一个参数,并且

{{ url_for('confirm') }}

它没有提供一个,而是以一种形式提供password,作为 POST 参数的一部分(一种不同的机制)。我想知道你是否不打算这样做

{{ url_for('recover') }}

反而。

于 2020-07-27T15:59:35.047 回答