我是 Pyramid 的新手,我正在遵循本指南: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/authorization.html
在 Django 中, View 和 URL 要么工作要么崩溃,但我从来没有遇到过在 views.py 和 urls.py 中明确定义的网页会出现 404 错误的情况,这从来没有发生在我身上。如果其中任何一个出现问题,它就会崩溃,而不是抛出 404。
我正在构建一个 wiki 页面来了解 Pyramid 上的内容,但在我继续学习教程之前我遇到了瓶颈。
我的 init.py 文件设置如下:
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
authn_policy = AuthTktAuthenticationPolicy(
'sosecret', callback=groupfinder, hashalg='sha512')
authz_policy = ACLAuthorizationPolicy()
config = Configurator(settings=settings,
root_factory='tutorial.models.RootFactory')
config.set_authentication_policy(authn_policy)
config.set_authorization_policy(authz_policy)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('view_wiki', '/')
config.add_route('view_page', '/{pagename}')
config.add_route('add_page', '/add_page/{pagename}')
config.add_route('edit_page', '/{pagename}/edit_page')
config.add_route('login', '/login')
config.add_route('logout', '/logout')
config.scan()
return config.make_wsgi_app()
我的 views.py 文件是这样设置的:
@view_config(route_name='login', renderer='templates/login.pt')
@forbidden_view_config(renderer='templates/login.pt')
def login(request):
login_url = request.route_url('login')
referrer = request.url
if referrer == login_url:
referrer = '/' # never use the login form itself as came_from
came_from = request.params.get('came_from', referrer)
message = ''
login = ''
password = ''
if 'form.submitted' in request.params:
login = request.params['login']
password = request.params['password']
if USERS.get(login) == password:
headers = remember(request, login)
return HTTPFound(location = came_from,
headers = headers)
message = 'Failed login'
return dict(
message = message,
url = request.application_url + '/login',
came_from = came_from,
login = login,
password = password,
)
@view_config(route_name='logout')
def logout(request):
headers = forget(request)
return HTTPFound(location = request.route_url('view_wiki'),
headers = headers)
到目前为止,应用程序运行良好,但登录和注销完全无效。我至少应该通过堆栈跟踪发生崩溃,但我只是得到了一个平坦的 404 错误...
我关闭了应用程序并重新运行 pserve development.ini --reload
什么也没有....他们只是不在那里....
我把它放在我的 URL 中:
0.0.0.0:6446/FrontPage <== WORKS
0.0.0.0:6446/login <== 404 Not Found, The resource could not be found. No such page
我不太确定如何调试声称不存在的东西...可能是什么问题?我将所有模板保存在同一个位置,并且将 edit_page View 与登录 View 进行比较,它们非常相同,但登录显然不存在......为什么?
最佳答案
尝试将登录名放在 /{pagename}
之前。该路线系统在遇到第一场比赛时停止并处理。反转所有其他以静态字符串开头的页面(例如:/foo
)
config.add_route('login', '/login')
config.add_route('logout', '/logout')
config.add_route('add_page', '/add_page/{pagename}')
config.add_route('view_page', '/{pagename}')
config.add_route('edit_page', '/{pagename}/edit_page')
关于python - Pyramid 登录和注销页面返回 404,应用程序的其余部分工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431888/