我有一个通过此功能实现的动态网址:
@results.route("/crawled/<domain_name>")
def show_results(domain_name):
#do some work
我正在尝试向用户发送一封包含此动态URL链接的电子邮件。这是发送电子邮件功能:
def send_email(email, domain_name):
with create_app().test_request_context():
msg = Message('Crawl Complete View the Results',
sender='noreply@demo.com',
recipients=[email])
msg.body = render_template('results_email.html',name=domain_name)
mail.send(msg)
这是我正在使用的电子邮件模板:
#results_email.html
The crawl for your requested domain is complete. To view the results visit the following link:
{{ url_for('results.show_results', domain_name=domain_name, _external=True) }}
If you did not make this request please ignore this email.
这里的问题是url_for
函数使url的路径成为完整的url。这是电子邮件示例:
The crawl for your requested domain is complete. To view the results visit the following link:
http://localhost/crawled/http://techxpecs.com/
If you did not make this request please ignore this email.
我无法弄清楚这是怎么回事。我对网站的“忘记密码”部分使用了类似的方法,并且url_for
在该网站上可以正常工作。它们之间的唯一区别是create_app().test_request_context()
。我必须在这里使用它,因为如果不使用url_for
会抛出此错误:
'Application was not able to create a URL adapter for request'
RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.
即使经过大量的搜索,我也无法弄清楚这里出了什么问题。