Christopher Spears
Christopher Spears

Reputation: 1105

Error in Pyramid todo web app

I've been working the Pyramid Todo list tutorial:

http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/single_file_tasks/single_file_tasks.html

My web app works, but I keep getting this error, which does not keep my app from running.

(pyramid_tutorial)christohersmbp2:pyramid_tutorial christopherspears$ python tasks/tasks.py
WARNING:tasks/tasks.py:Initializing database...
127.0.0.1 - - [23/Mar/2014 18:59:23] "GET / HTTP/1.1" 200 891
127.0.0.1 - - [23/Mar/2014 18:59:23] "GET /static/style.css HTTP/1.1" 304 0
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/router.py", line 272, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/router.py", line 247, in invoke_subrequest
    response = handle_request(request)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/tweens.py", line 46, in excview_tween
    response = view_callable(exc, request)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/config/views.py", line 287, in _authdebug_view
    return view(context, request)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/config/views.py", line 377, in rendered_view
    context)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/renderers.py", line 416, in render_view
    return self.render_to_response(response, system, request=request)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/renderers.py", line 439, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/renderers.py", line 435, in render
    result = renderer(value, system_values)
  File "/Users/christopherspears/.virtualenvs/pyramid_tutorial/lib/python2.7/site-packages/pyramid_mako-1.0a2-py2.7.egg/pyramid_mako/__init__.py", line 122, in __call__
    raise ValueError('renderer was passed non-dictionary as value')
ValueError: renderer was passed non-dictionary as value

I'm not sure what is causing this because I don't see my code listed in the traceback. Here is the code I wrote for the tutorial:

import os
import logging

from pyramid.config import Configurator
from pyramid.session import UnencryptedCookieSessionFactoryConfig

from pyramid.events import NewRequest
from pyramid.events import subscriber
from pyramid.events import ApplicationCreated
import sqlite3

from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from pyramid.view import view_config

from wsgiref.simple_server import make_server

logging.basicConfig()
log = logging.getLogger(__file__)

here = os.path.dirname(os.path.abspath(__file__))

@subscriber(ApplicationCreated)
def application_created_subscriber(event):
  log.warn('Initializing database...')
  with open(os.path.join(here, 'schema.sql')) as f:
    stmt = f.read()
    settings = event.app.registry.settings
    db = sqlite3.connect(settings['db'])
    db.executescript(stmt)

@subscriber(NewRequest)
def new_request_subscriber(event):
  request = event.request
  settings = request.registry.settings
  request.db = sqlite3.connect(settings['db'])
  request.add_finished_callback(close_db_connection)

def close_db_connection(request):
  request.db.close()

@view_config(route_name='list', renderer='list.mako')
def list_view(request):
  rs = request.db.execute("select id, name from tasks where closed = 0")
  tasks = [dict(id=row[0], name=row[1]) for row in rs.fetchall()]
  return {'tasks': tasks}

@view_config(route_name='new', renderer='new.mako')
def new_view(request):
  if request.method == 'POST':
    if request.POST.get('name'):
      request.db.execute(
        'insert into tasks (name, closed) values (?, ?)',
        [request.POST['name'], 0])
      request.db.commit()
      request.session.flash('New task was successfully added!')
      return HTTPFound(location=request.route_url('list'))
    else:
      request.session.flash('Please enter a name for the task')
  return {}

@view_config(route_name='close')
def close_view(request):
  task_id = int(request.matchdict['id'])
  request.db.execute("update tasks set closed = ? where id = ?",
                    (1, task_id))
  request.db.commit()
  request.session.flash('Task was successfully closed!')
  return HTTPFound(location=request.route_url('list'))

@view_config(context='pyramid.exceptions.NotFound', renderer='notfound.mako')
def notfound_view(request):
  request.response.status = '404 Not Found'


if __name__ == '__main__':

  # configuration settings
  settings = {}
  settings['reload_all'] = True
  settings['debug_all'] = True
  settings['db'] = os.path.join(here, 'tasks.db')
  settings['mako.directories'] = os.path.join(here, 'templates')

  # session factory 
  session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')

  # configuration setup
  config = Configurator(settings=settings, session_factory=session_factory)
  config.scan()
  config.include('pyramid_mako')
  config.add_static_view('static', os.path.join(here, 'static'))
  config.add_route('list', '/')
  config.add_route('new', '/new')
  config.add_route('close', '/close/{id}')

  # serve app
  app = config.make_wsgi_app()
  server = make_server('0.0.0.0', 8080, app)
  server.serve_forever()

Any idea what the issue is? I probably just need a fresh pair of eyes on this.

Upvotes: 0

Views: 306

Answers (1)

Sergey
Sergey

Reputation: 12437

Your not_found_view does not return a dict. If you check the Network page in Firebug/Chrome dev tools you'll see that your page makes a request to some non-existing resource (favicion.ico?) - that should result in the not_found_view being invoked, but because of the exception the request results in 501 response instead of 404.

Upvotes: 3

Related Questions