当然,我讨厌使用忙碌的StackExchange成员的时间来解决我的问题,但我已经在论坛中询问我检索此代码以解释该机制是如何工作的,但是一直没有回应。
我的问题应该很简单(不适合我自己,因为我就是你所谓的网络开发爱好者)。我有这个代码(不是我自己的)用于后端的基本博客。但有一点我不太了解。在其中一个HTML文件中,有一行{{user.name}}
,其中包含当前登录人员的用户名,并将其发布在页面的一角。 {{}}
只是Jinja2语法,它将python表达式打印到模板中(或者我被告知)。
如何获取在Python文件中登录的用户的用户名?我目前在自己的网络应用程序中尝试做的是获取登录用户的用户名和查询Google数据存储区以获取有关用户的信息并将其放入个人资料页面,如下所示:
class Profile(BlogHandler):
def render_profile(self):
signatory = user.name
signedevents = db.GqlQuery("SELECT event FROM Signatories WHERE username IN (signatory)")
self.render("profile.html", signedevents=signedevents)
def get(self):
if self.user:
self.render_profile()
else:
self.redirect('/')
但每当我尝试这个时,我总会得到一个错误。是否还有其他东西应该代替user.name
?
博客代码(我确定答案在Handler类的某个地方,但我根本无法弄清楚问题是什么):
import os
import re
import random
import hashlib
import hmac
from string import letters
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
secret = 'fart'
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def make_secure_val(val):
return '%s|%s' % (val, hmac.new(secret, val).hexdigest())
def check_secure_val(secure_val):
val = secure_val.split('|')[0]
if secure_val == make_secure_val(val):
return val
class BlogHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
params['user'] = self.user
return render_str(template, **params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def set_secure_cookie(self, name, val):
cookie_val = make_secure_val(val)
self.response.headers.add_header(
'Set-Cookie',
'%s=%s; Path=/' % (name, cookie_val))
def read_secure_cookie(self, name):
cookie_val = self.request.cookies.get(name)
return cookie_val and check_secure_val(cookie_val)
def login(self, user):
self.set_secure_cookie('user_id', str(user.key().id()))
def logout(self):
self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/')
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
self.user = uid and User.by_id(int(uid))
def render_post(response, post):
response.out.write('<b>' + post.subject + '</b><br>')
response.out.write(post.content)
class MainPage(BlogHandler):
def get(self):
self.write('Hello, Udacity!')
##### user stuff
def make_salt(length = 5):
return ''.join(random.choice(letters) for x in xrange(length))
def make_pw_hash(name, pw, salt = None):
if not salt:
salt = make_salt()
h = hashlib.sha256(name + pw + salt).hexdigest()
return '%s,%s' % (salt, h)
def valid_pw(name, password, h):
salt = h.split(',')[0]
return h == make_pw_hash(name, password, salt)
def users_key(group = 'default'):
return db.Key.from_path('users', group)
class User(db.Model):
name = db.StringProperty(required = True)
pw_hash = db.StringProperty(required = True)
email = db.StringProperty()
@classmethod
def by_id(cls, uid):
return User.get_by_id(uid, parent = users_key())
@classmethod
def by_name(cls, name):
u = User.all().filter('name =', name).get()
return u
@classmethod
def register(cls, name, pw, email = None):
pw_hash = make_pw_hash(name, pw)
return User(parent = users_key(),
name = name,
pw_hash = pw_hash,
email = email)
@classmethod
def login(cls, name, pw):
u = cls.by_name(name)
if u and valid_pw(name, pw, u.pw_hash):
return u
此处的所有内容可能并不重要,但如果仅使用上述代码无法找到答案,请继续。
##### blog stuff
def blog_key(name = 'default'):
return db.Key.from_path('blogs', name)
class Post(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
last_modified = db.DateTimeProperty(auto_now = True)
def render(self):
self._render_text = self.content.replace('\n', '<br>')
return render_str("post.html", p = self)
class BlogFront(BlogHandler):
def get(self):
posts = greetings = Post.all().order('-created')
self.render('front.html', posts = posts)
class PostPage(BlogHandler):
def get(self, post_id):
key = db.Key.from_path('Post', int(post_id), parent=blog_key())
post = db.get(key)
if not post:
self.error(404)
return
self.render("permalink.html", post = post)
class NewPost(BlogHandler):
def get(self):
if self.user:
self.render("newpost.html")
else:
self.redirect("/login")
def post(self):
if not self.user:
self.redirect('/blog')
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
p = Post(parent = blog_key(), subject = subject, content = content)
p.put()
self.redirect('/blog/%s' % str(p.key().id()))
else:
error = "subject and content, please!"
self.render("newpost.html", subject=subject, content=content, error=error)
###### Unit 2 HW's
class Rot13(BlogHandler):
def get(self):
self.render('rot13-form.html')
def post(self):
rot13 = ''
text = self.request.get('text')
if text:
rot13 = text.encode('rot13')
self.render('rot13-form.html', text = rot13)
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
return username and USER_RE.match(username)
PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):
return password and PASS_RE.match(password)
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
return not email or EMAIL_RE.match(email)
class Signup(BlogHandler):
def get(self):
self.render("signup-form.html")
def post(self):
have_error = False
self.username = self.request.get('username')
self.password = self.request.get('password')
self.verify = self.request.get('verify')
self.email = self.request.get('email')
params = dict(username = self.username,
email = self.email)
if not valid_username(self.username):
params['error_username'] = "That's not a valid username."
have_error = True
if not valid_password(self.password):
params['error_password'] = "That wasn't a valid password."
have_error = True
elif self.password != self.verify:
params['error_verify'] = "Your passwords didn't match."
have_error = True
if not valid_email(self.email):
params['error_email'] = "That's not a valid email."
have_error = True
if have_error:
self.render('signup-form.html', **params)
else:
self.done()
def done(self, *a, **kw):
raise NotImplementedError
class Unit2Signup(Signup):
def done(self):
self.redirect('/unit2/welcome?username=' + self.username)
class Register(Signup):
def done(self):
#make sure the user doesn't already exist
u = User.by_name(self.username)
if u:
msg = 'That user already exists.'
self.render('signup-form.html', error_username = msg)
else:
u = User.register(self.username, self.password, self.email)
u.put()
self.login(u)
self.redirect('/blog')
class Login(BlogHandler):
def get(self):
self.render('login-form.html')
def post(self):
username = self.request.get('username')
password = self.request.get('password')
u = User.login(username, password)
if u:
self.login(u)
self.redirect('/blog')
else:
msg = 'Invalid login'
self.render('login-form.html', error = msg)
class Logout(BlogHandler):
def get(self):
self.logout()
self.redirect('/blog')
class Unit3Welcome(BlogHandler):
def get(self):
if self.user:
self.render('welcome.html', username = self.user.name)
else:
self.redirect('/signup')
class Welcome(BlogHandler):
def get(self):
username = self.request.get('username')
if valid_username(username):
self.render('welcome.html', username = username)
else:
self.redirect('/unit2/signup')
app = webapp2.WSGIApplication([('/', MainPage),
('/unit2/rot13', Rot13),
('/unit2/signup', Unit2Signup),
('/unit2/welcome', Welcome),
('/blog/?', BlogFront),
('/blog/([0-9]+)', PostPage),
('/blog/newpost', NewPost),
('/signup', Register),
('/login', Login),
('/logout', Logout),
('/unit3/welcome', Unit3Welcome),
],
debug=True)
如果有什么需要我澄清,请告诉我。提前感谢您的帮助。
答案 0 :(得分:1)
好的,你继承了BlogHandler
。
要获取用户名信息,请按照Unit3Welcome()
示例,我认为您需要将signatory = user.name
更改为signatory = self.user.name
要填充jinja2模板中的{{user.name}}
字段,需要输入模板值dict
,它会收到一个user
变量(也是dict
类型的变量),其中包含name
密钥(可能使用相应的用户名)。例如:{'user': {'name': 'johndoe'}}
。
我既不熟悉您使用的渲染器如何工作,也不熟悉SQL以及填充signedevents
的方式,因此您可能需要进行调整。
为了方便地检查(在调试打印样式中)实际使用到模板的内容,您可以在html:<p>user: ({{ user }})</p>
中的某个位置临时插入这样的段落,并且您应该获得user
的转储在您的浏览器中变量:)
答案 1 :(得分:0)
嗨我记得App Engine的这个东西,也许它必须用旧的&db;#39;数据存储。 Google现在建议使用&#39; ndb&#39;。 由于您还设置了cookie,因此您可以使用它在欢迎页面中呈现用户的名称。
这就是我如何解决它,希望这会有所帮助。
class Register(SignUp):
def done(self):
#make sure the user doesn't already exist
u = User.by_name(self.request.get("username"))
if u:
msg = 'That user already exists.'
self.render('signup-form.html', username_exists = msg)
else:
username = self.request.get("username")
password = self.request.get("password")
email = self.request.get("email")
u = User.register(username, password , email)
u.put()
self.login(u)
self.redirect('/blog/welcome')
class Welcome(BlogHandler):
def get(self):
cookie = self.request.cookies.get("user_id")
val = check_secure_val(cookie)
u = User.by_id(int(val))
if u:
print u
self.render('welcome.html', username = u.name)
else:
self.redirect('/blog/signup')