python - flask_mail 中的_external=True 是什么意思?

标签 python web flask project flask-mail

import os
import secrets
from PIL import Image
from flask import url_for, current_app
from flask_mail import Message
from app import mail


def save_picture(form_picture):
    random_hex = secrets.token_hex(8)
    f_name, f_ext = os.path.splitext(form_picture.filename)
    picture_fn = random_hex + f_ext
    picture_path = os.path.join(current_app.root_path, 'static/profile_pics', picture_fn)
    output_size = (125, 125)
    i = Image.open(form_picture)
    i.thumbnail(output_size)
    i.save(picture_path)
    return picture_fn

def send_reset_email(user):
    token = user.get_reset_token()
    msg = Message('Password Reset Request', sender='noreplies@diemo.hr', recipients=[user.email])
    msg.body = f''' To reset your password, visit the following link:
{url_for('users.reset_token', token=token, **_external=True**)}
If you did not make this request then simply ignore this email and no changes will be made
    '''
    mail.send(msg)

我正在制作一个 Flask 应用程序并学习如何使用 flask_mail 模块发送重置电子邮件,所以我想知道当我将重置链接放在邮件正文中时 _external=True(send_reset_email function) 是什么意思。我在谷歌上搜索过,但我什么也没找到。提前谢谢你。

最佳答案

_external=True 告诉 Flask 它应该生成一个绝对 URL,而不是一个相对 URL。例如,https://example.com/my-page 是绝对 URL,而 /my-page 是相对 URL。由于您发送的是电子邮件,因此您站点中某个页面的相对 URL 将不起作用。

您可以在此处查看 url_for 的文档:https://flask.palletsprojects.com/en/1.1.x/api/#flask.url_for

_external – if set to True, an absolute URL is generated. Server address can be changed via SERVER_NAME configuration variable which falls back to the Host header, then to the IP and port of the request.

关于python - flask_mail 中的_external=True 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61306257/

相关文章:

python - 在Python中模拟多个给定多个概率的最快方法?

python - 如何一致地缩放数据帧 MinMaxScaler() sklearn

python - TIA Bloomberg 请求处理无效代码

python - 耗时 flask 的装饰器

mysql - "from flaskext.mysql import MySQL"导致服务器显示 502 bad gateway

python - 如何在 Flask 中生成临时下载?

python - 如何使用 tkinter 将框架放在框架内?

url - 在 Telegram 中检索网站信息的网站结构应该是什么?

java - Web如何防止修改元素属性值引起的安全问题?如禁用或隐藏

javascript - 函数不适用于 setTimeout