【问题标题】:Flask Webpage Via Cron Job - url_for call broken通过 Cron 作业的烧瓶网页 - url_for 调用中断
【发布时间】:2014-10-22 02:56:45
【问题描述】:

我的 Flask 应用程序中有一个脚本,每 5 分钟执行一次。出于很多原因。但这没关系。

当我运行此代码时,我现在希望在此网页中包含指向我的 app.py Flask 应用中的函数的链接。

app.py

@app.route('/Historical-Service-Transitions/<service>')
@login_required
@nocache
def HSPC(service):
    return service

kickoff.py,按计划运行。

from jinja2 import Template
import paramiko 
import socket
import time
import pprint
import sys
import mysql.connector
from mysql.connector import errorcode
from collections import namedtuple
import datetime
from app import HSPC
from flask import url_for


source_html =Template(u'''
....#The part that is failing
<tbody>
{% for x in the_best %}
    <tr>
        <td><a href="{{ url_for('HSPC', service ='x.service') }}">{{x.service}}</a></td>
        <td>{{x.ip}}</td>
        <td>{{x.router}}</td>
        <td>{{x.detail}}</td>
        <td>{{x.time}}</td>
    </tr>
{% endfor %}
</tbody>
....''')

full_html = source_html.render(
the_best=the_best,
the_time=the_time
)


write_that_sheet = open("/var/www/flask-intro/templates/Test.html", "w+")
write_that_sheet.write(full_html)
write_that_sheet.close()

错误

Traceback (most recent call last):
  File "kickoff.py", line 1199, in <module>
    the_time=the_time
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.6/site-packages/Jinja2-2.7.3-py2.6.egg/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 534, in top-level template code
jinja2.exceptions.UndefinedError: 'url_for' is undefined

任何帮助将不胜感激。


更新:

我什至无法找到任何与我正在尝试做的事情相近的事情。我知道我可以重建它,以便后台 Python 代码获取信息,而我的 app.py 构建 HTML。后台应用程序会填充数据库,然后app.py 中的函数会从数据库中获取所需的信息并发布到 HTML 页面。

虽然这是最后的手段,因为它需要我重新设计应用程序的整个部分,但我仍然想看看是否有解决方案可以让我在 app.py Flask 之外生成这个网页。

【问题讨论】:

    标签: python cron flask jinja2 url-for


    【解决方案1】:

    您缺少 Flask 提供的 Jinja 上下文。使用flask.render_template_string() 渲染在字符串中定义的模板,将为您提供正确的模板上下文:

    from flask import render_template_string
    
    source_html = u'''
    <tbody>
    {% for x in the_best %}
        <tr>
            <td><a href="{{ url_for('HSPC', service ='x.service') }}">{{x.service}}</a></td>
            <td>{{x.ip}}</td>
            <td>{{x.router}}</td>
            <td>{{x.detail}}</td>
            <td>{{x.time}}</td>
        </tr>
    {% endfor %}
    </tbody>
    '''
    
    filename = "/var/www/flask-intro/templates/Test.html"
    
    # provide a fake request context for the template
    with app.test_request_context('/'), open(filename, "w") as outfh:
        full_html = render_template_string(
            source_html, the_best=the_best, the_time=the_time)
        outfh.write(full_html)
    

    但是,对于定期作业,您可以只需使用 curl 在 Flask 应用中调用一个特殊的 URL,让它将模板输出写入文件。

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多