【问题标题】:How do I use url_for in wtf before the app has initialized?在应用程序初始化之前,如何在 wtf 中使用 url_for?
【发布时间】:2020-02-16 15:51:03
【问题描述】:

我想知道如何在应用程序初始化之前在 wtf 表单类中使用flask 中的url_for() 函数。 我的表单类如下所示:

class UserRegisterForm(FlaskForm):
    accept_terms = BooleanField(
        "I have read the <a href=\"{{ url_for('terms_of_service') }}\">terms of service</a><br> and I 
        agree with them.", validators=[DataRequired()])

现在,你看,我正在尝试在标签文本中使用url_for() 函数。不幸的是,这不起作用。 我无法在标签之外使用该功能,因为我收到了一个 rumtime 错误,因为我在生成网站之前使用了该功能。 谁能解决这个问题?

【问题讨论】:

    标签: python python-3.x flask jinja2 flask-wtforms


    【解决方案1】:

    您可以在标签之外创建网址:

    terms_url = url_for('terms_of_service')
    
    accept_terms = BooleanField(f"I have read the {terms_url}...")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 2022-07-22
      • 2015-06-05
      • 2018-01-16
      相关资源
      最近更新 更多