Alexander McNulty
Alexander McNulty

Reputation: 890

pyramid.httpexceptions.HTTPNotFound: The resource could not be found

I am working off of Safari's Pyramid tutorial

WEB APPLICATIONS WITH PYTHON AND THE PYRAMID FRAMEWORK

Inside of my views.py file I having a problem with the following code:

@property
def current(self):
    todo_id = self.request.matchdict.get('id')
    todo = sample_todos.get(todo_id)
    if not todo:
        raise HTTPNotFound()
    return todo

particularly when the following view function calls this property

@view_config(route_name='view', renderer='templates/view.jinja2')
def view(self):
    return dict(todo=self.current)

when I am running the application http://0.0.0.0:6543/5 will not trigger the anticipated HTTPNotFound(), see route below.

config.add_route('view', '/{id}')

the error logs return:

  File "/Users/alex/zdev/t-oreilly/mysite/views.py", line 50, in view
    return dict(todo=self.current)
  File "/Users/alex/zdev/t-oreilly/mysite/views.py", line 25, in current
    raise HTTPNotFound()
pyramid.httpexceptions.HTTPNotFound: The resource could not be found.

On the browser waitress returns a default server error.

What is the proper way to remove this error?


I have uploaded this work to github, commit aaf562e

the tutorial link is here, for those eager to help, it can be accessed with their 10 day trial. This problem is from video 17/48.

thank you, if you need additional information please let me know.

Upvotes: 1

Views: 3077

Answers (2)

Paul Merrill
Paul Merrill

Reputation: 620

In two of your Jinja templates you reference the @property view.current. However, since the property throws an HTTPNotFound() exception, your Jinja templates end up hitting that and explode, causing your problem.

Either remove the calls to view.current from your Jinja templates or modify your view.current function so that it doesn't throw.

I'm not sure if this is the solution you are looking for, but it doesn't deviate from the tutorial.

Upvotes: 1

Michael Merickel
Michael Merickel

Reputation: 23331

This is a different HTTPNotFound exception and it is raised at the route-matching step before your view is even executed. The reason is that you have config.add_route('view', '/{id}'). Note the /{id} NOT /{id}/. Pyramid considers these two different routes and thus the latter does not match. The simplest solution to this is to register all of our canonical routes with a / suffix such as /{id}/ and then pass append_slash=True to your notfound view configuration such as config.add_notfound_view(..., append_slash=True) or @notfound_view_config(append_slash=True). This will trigger a redirect when a user visits the version without the trailing slash.

Upvotes: 3

Related Questions