我正在我的App Engine(Python 2.7)应用程序上实现Simpleauth的Simpleauth:https://github.com/crhym3/simpleauth而且我无法弄清楚为什么我会收到404错误。
当我在未登录时尝试转到'/ profile'时,我会正确地重定向到'/'。但是当我登录(成功)并被定向到'/ profile'时,我得到了404。
我认为这是因为它在'render'代码中遇到了“TemplateNotFound”异常。但是“profile.html”文件存在并且正确地位于“模板”目录中。
有谁能帮我弄清楚我做错了什么?
以下是相关文件的代码:
的app.yaml
handlers:
- url: /auth/*
script: main.app
- url: /profile
script: main.app
- url: /logout/*
script: main.app
- url: /.*
script: main.app
libraries:
- name: jinja2
version: latest
main.py
import webapp2
import jinja2
from secrets import SESSION_KEY
template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd()))
class MainHandler(webapp2.RequestHandler):
def get(self):
template = template_env.get_template('templates/home.html')
context = {
}
self.response.out.write(template.render(context))
app_config = {
'webapp2_extras.sessions': {
'cookie_name': '_simpleauth_sess',
'secret_key': SESSION_KEY
},
'webapp2_extras.auth': {
'user_attributes': []
}
}
app = webapp2.WSGIApplication([('/', MainHandler),
webapp2.Route('/profile', handler='handlers.ProfileHandler', name='profile'),
webapp2.Route('/auth/<provider>', handler='handlers.AuthHandler:_simple_auth', name='auth_login'),
webapp2.Route('/auth/<provider>/callback', handler='handlers.AuthHandler:_auth_callback', name='auth_callback'),
webapp2.Route('/logout', handler='handlers.AuthHandler:logout', name='logout')], config=app_config, debug=True)
handlers.py (与Alex的Simpleauth代码保持不变!)
import webapp2
from webapp2_extras import auth, sessions, jinja2
from jinja2.runtime import TemplateNotFound
from simpleauth import SimpleAuthHandler
class BaseRequestHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def jinja2(self):
"""Returns a Jinja2 renderer cached in the app registry"""
return jinja2.get_jinja2(app=self.app)
@webapp2.cached_property
def session(self):
"""Returns a session using the default cookie key"""
return self.session_store.get_session()
@webapp2.cached_property
def auth(self):
return auth.get_auth()
@webapp2.cached_property
def current_user(self):
"""Returns currently logged in user"""
user_dict = self.auth.get_user_by_session()
return self.auth.store.user_model.get_by_id(user_dict['user_id'])
@webapp2.cached_property
def logged_in(self):
"""Returns true if a user is currently logged in, false otherwise"""
return self.auth.get_user_by_session() is not None
def render(self, template_name, template_vars={}):
# Preset values for the template
values = {
'url_for' : self.uri_for,
'logged_in' : self.logged_in
}
# Add manually supplied template values
values.update(template_vars)
# read the template or 404.html
try:
self.response.write(self.jinja2.render_template(template_name, **values))
except TemplateNotFound:
self.abort(404)
def head(self, *args):
"""Head is used by Twitter. If not there the tweet button shows 0"""
pass
class ProfileHandler(BaseRequestHandler):
def get(self):
"""Handles GET /profile"""
if self.logged_in:
self.render('profile.html', {
'user': self.current_user,
'session': self.auth.get_user_by_session()
})
else:
self.redirect('/')
答案 0 :(得分:1)
上面的问题是我使用两种不同的方式来实现Jinja2。在另一个问题中讨论了两种不同的方法:Which is the preferred method to use jinja2 on App Engine?
导致404错误的是混合和匹配。