我正在为 Python 使用 Bottle 框架(也使用 Beaker 进行会话),并且无法从 HTML 代码中的脚本访问变量。以下是 Python 脚本:
import os, MySQLdb, hashlib, random, markdown2
import beaker.middleware
import bottle
from bottle import run, route, post, get, request, abort, template, hook, app, view
@post('/submit')
def submit():
db = MySQLdb.connect(host='localhost', port=3306, user="root", passwd="blkFDF94alkf", db="_pCMS")
query = db.cursor()
user = request.forms.get('credentials.username')
username = MySQLdb.escape_string(user)
request.session['username'] = username
passw = request.forms.get('credentials.password')
pass_w = MySQLdb.escape_string(passw)
passw2 = str(pass_w)
password = hashlib.md5(passw2).hexdigest()
user_name = request.session['username']
if username >= 2 and password >= 6:
if True:
ugh = query.execute("SELECT * FROM users WHERE username = '%s' AND password = '%s'" % (username, password))
db.commit()
return me()
else:
return index()
我怀疑上面的代码是否需要解释,因为我只需要知道如何在下面的 HTML 代码上访问那个 Python 脚本的函数中的变量 user_name。这就是我所拥有的 HTML:
我.tpl:
<div class="label">Name:</div>
%if len(user_name) >= 2:
<div class="content">{{user_name}}</div>
%end
</div>
这是访问变量 user_name 的正确方法吗?它不是那样工作的,因为它给了我以下错误:
NameError: name 'user_name' is not defined
me() 和 index() 做什么:
@route('/')
@route('/index')
@view('index.tpl')
def index():
index = { 'index' : _index()}
return index
def _index():
return 't'
@get('/me')
@view('me.tpl')
def me():
me = { 'me' : _me()}
return me
def _me():
return 't'