pyramidpyramid-debug-toolbar

possible weird bug in pyramid web framework


I was following pyramid web framework tutorial steps given in the link: https://docs.pylonsproject.org/projects/pyramid/en/latest/quick_tutorial/cookiecutters.html

After setting it up and visiting http://localhost:6543/ everything works as expected with the project name "Pyramid scaffold" in the route name showing properly.

Then I added a second view function and added it to the route. But then the home route starts showing 404. The second route works, but the first route and view stop working and gives 404 when loaded in the browser.

I cannot find what the issue is. After adding several functions and routes, I was not able to find the issue. I am thinking this is some issue with provided cookiecutter or pyramid framework itself. This never used to happen with pyramid version less than 2. Also tried adding different views and routes. Only one route seems to work and all others return 404 exception.

No files were deleted or edited other than the ones listed here.

Can someone please help me with this?

Original contents of files

# File location 'views/default.py"

from pyramid.view import view_config


@view_config(route_name='home', renderer='pyramid_scaffold:templates/mytemplate.jinja2')
def my_view(request):
    return {'project': 'Pyramid Scaffold'}

and

# File location 'routes.py"


def includeme(config):
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')

After my changes

# File location 'views/default.py"

from pyramid.view import view_config


@view_config(route_name='home', renderer='pyramid_scaffold:templates/mytemplate.jinja2')
def my_view(request):
    return {'project': 'Pyramid Scaffold'}


@view_config(route_name='second', renderer='pyramid_scaffold:templates/mytemplate.jinja2')
def my_view(request):
    return {'project': 'this works'}

and

# File location 'routes.py"


def includeme(config):
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('second', '/second')

error log in terminal:

2021-10-01 02:41:29,880 INFO  [pyramid_debugtoolbar:287][waitress-0] Squashed pyramid.httpexceptions.HTTPNotFound at http://localhost:6543/
traceback url: http://localhost:6543/_debug_toolbar/313430323330373531313831343038/exception

When visiting traceback URL, no helpful info other than saying

env/lib/python3.8/site-packages/pyramid/router.py", line 169, in handle_request
raise HTTPNotFound(msg)

Solution

  • Bugs are almost always in the developer's code, and rarely in a mature package such as Pyramid.

    In your case, you defined two methods with the same name, overriding the first with the second. Therefore the view for the first route home was removed.

    To remedy the situation, give the second view function a unique name.

    @view_config(route_name='second', renderer='pyramid_scaffold:templates/mytemplate.jinja2')
    def my_second_view(request):
        return {'project': 'this works'}