Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid login and logout page return 404, the rest of the app works fine

Tags:

python

pyramid

I'm new to Pyramid and I'm following this guide: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/authorization.html

In Django, Views and URLs either worked or they crashed, but I've never run into a situation where a webpage that's clearly defined in both the views.py and urls.py would come up with a 404 error, this never happened to me. If there was something wrong with either of them it would crash, not throw a 404.

I'm building a wiki page to get the swing of things on Pyramid but I've hit a wall before I can move on with the tutorial.

I have my init.py file setup like this:

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()

And my views.py file is setup like this:

@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)

And so far the application runs great, but the login and logout are just completely void. I should at LEAST get a crash with a stack trace but I'm just given a flat out 404 ERROR...

I shut down the app and reran pserve development.ini --reload and nothing.... they're just not there....

I put this in my 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

I'm not really sure how to debug something that claims to be non existant... What could be the problem? I'm keeping all the templates in the same location and I'm comparing the edit_page view with the login view and they're extremely identical, but login doesn't exist apparently.... Why?

like image 239
user1504605 Avatar asked Mar 16 '14 01:03

user1504605


1 Answers

Try put login before /{pagename}. This route system is stop on first match is encounter and process this. Invert all other pages start with static string (Ex.: /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')
like image 135
Jones Avatar answered Oct 13 '22 10:10

Jones