【发布时间】: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