我有一个带蓝图结构的模块化应用程序。我注册了蓝图,他们工作正常。我遇到了带参数的url_for问题:
werkzeug.routing.BuildError:无法使用值['token']
为端点'confirm_email'构建网址这是我在 home.py
中的端点@home.route('/register',methods=['GET','POST'])
def register():
form = RegistrationForm(request.form)
print(form, file=sys.stderr)
print(form.email, file=sys.stderr)
if request.method == 'POST' and form.validate():
u = User(
email = form.email.data,
password = form.password.data
)
email_ddbb = User.query.filter_by(email=form.email.data).first()
# we register the user
if email_ddbb is None:
# Now we'll send the email confirmation link
subject = "Confirm your email"
token = ts.dumps(u.email, salt='email-confirm-key')
print(url_for('user.activate',token=token,_external=True))
confirm_url = url_for('user.activate',token=token,_external=True)
html = render_template('mail/activate.html', confirm_url=confirm_url)
# we send the email from utils.py
send_email(u.email, subject, html)
db.session.add(u)
db.session.commit()
flash('Thanks for registering, please check your inbox')
return redirect(url_for('home.index'))
else:
flash('Email already in use')
return redirect(url_for('home.index'))
#TODO MUST BE UNIQUE
else:
flash('Please enter a valid email address')
return redirect(url_for('home.index'))
这是 user.py
中的另一个端点@user.route('/confirm/<token>')
def activate(token):
try:
email = ts.loads(token, salt="email-confirm-key", max_age=86400)
except:
abort(404)
user = User.query.filter_by(email=email).first_or_404()
user.verified = True
db.session.add(user)
db.session.commit()
return redirect(url_for('home.index'))
答案 0 :(得分:0)
Okey,问题不在那里,而是在邮件中:
def send_email(to, subject, template, **kwargs):
msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject,
sender=app.config['MAIL_SENDER'], recipients=[to])
msg.body = ''
token = ts.dumps(to, salt='email-confirm-key')
confirm_url = url_for(
'confirm_email', <-- should be user.activate
token=token,
_external=True)
msg.html = render_template(
'activate.html',
confirm_url=confirm_url)
mail.send(msg)