def edit():
site_info = site_get()
article_id = request.args.get('article_id',0)
temp_dict = {}
packet_dict = {}
packet_list = Packet.query.filter_by().all()
if packet_list is not None:
for temp in packet_list:
temp_dict = temp.__dict__
del temp_dict["_sa_instance_state"]
packet_dict[str(temp_dict['id'])] = temp_dict['packet_name']
#packet_dict = dict( packet_dict.items() + tempdict.items() )
if article_id != 0:
article = Article.query.filter_by(id = article_id).first()
#print article
if article is not None:
article = article.__dict__
title = article['title']
packet_id = article['packet_id']
show = article['show']
body = article['body']
return render_template('admin/edit.html', **locals())
python类render_template()的实例源码
def outputjson():
site_info = site_get()
tempdict = {}
tempjson = "["
info_list = Article.query.filter_by().all()
for item in info_list:
tempdict = item.__dict__
del tempdict["_sa_instance_state"]
value = json.dumps(tempdict,cls=CJsonEncoder)
tempjson += value + ",\n"
tempjson = tempjson[:-2] + "]"
filename = 'page_list_'+str(time.strftime("%Y%m%d"))+'.txt'
output = open(filename,'w')
output.write(tempjson)
output.close()
flash(u'?????????????')
return render_template('admin/output.html', **locals())
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_template_rendered(self):
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('simple_template.html', whiskey=42)
recorded = []
def record(sender, template, context):
recorded.append((template, context))
flask.template_rendered.connect(record, app)
try:
app.test_client().get('/')
self.assert_equal(len(recorded), 1)
template, context = recorded[0]
self.assert_equal(template.name, 'simple_template.html')
self.assert_equal(context['whiskey'], 42)
finally:
flask.template_rendered.disconnect(record, app)
def test_memory_consumption(self):
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('simple_template.html', whiskey=42)
def fire():
with app.test_client() as c:
rv = c.get('/')
self.assert_equal(rv.status_code, 200)
self.assert_equal(rv.data, b'<h1>42</h1>')
# Trigger caches
fire()
# This test only works on CPython 2.7.
if sys.version_info >= (2, 7) and \
not hasattr(sys, 'pypy_translation_info'):
with self.assert_no_leak():
for x in range(10):
fire()
def home():
return render_template('home.html')
def newletter():
return render_template('upload.html')
def deliver():
return render_template('deliver.html')
def too_big(error):
return render_template('toobig.html'), 413
def instructions():
return render_template('quick.html')
def questions():
return render_template('faq.html')
def whitelist():
with io.open('static/whitelist.txt', 'r', encoding="utf-8") as f: #gets the contents of whitelist.txt so they can be displayed
data = f.read().replace('@', ' [at] ').replace('.', ' [dot] ')
return render_template('whitelist.html',data=data)
def thanks():
return render_template('thanks.html')
def credits():
return render_template('credits.html')
def unsubscribe(email):
return render_template('unsubscribe.html',email=email)
def admin():
return render_template('admin.html')
def tail():
if request.method == 'POST':
fi = request.form['file']
if os.path.isfile(fi):
n = int(request.form['n'])
le = io.open(fi, 'r', encoding='utf-8')
taildata = le.read()[-n:]
le.close()
else:
taildata = "No such file."
return render_template('tail.html',taildata=taildata)
def wladd():
if request.method == 'POST':
addr = request.form['addr'].lstrip().rstrip()
f = io.open('static/whitelist.txt', 'a', encoding="utf-8")
f.write(addr.decode('utf-8') + u'\r\n')
f.close()
return render_template('wladd.html')
def unsub():
if request.method == 'POST':
addr = request.form['addr'].lstrip().rstrip()
f = io.open('unsubscribers.txt', 'a', encoding="utf-8")
f.write(addr.decode('utf-8') + u'\r\n')
f.close()
f = io.open('static/whitelist.txt', 'r', encoding="utf-8")
lines = f.readlines()
f.close()
f = io.open('static/whitelist.txt', 'w', encoding="utf-8")
for line in lines:
if addr not in line:
f.write(line.decode('utf-8'))
f.close()
return render_template('unsubscribed.html',addr=addr)