Pyramid todo网络应用程序出错

时间:2014-03-24 02:23:58

标签: python pyramid

我一直在使用Pyramid Todo列表教程:

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

我的网络应用有效,但我一直收到此错误,导致我的应用无法运行。

(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

我不确定导致这种情况的原因是因为我没有看到我在回溯中列出的代码。这是我为教程编写的代码:

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

知道问题是什么吗?我可能只需要一双新鲜的眼睛。

1 个答案:

答案 0 :(得分:3)

你的not_found_view没有返回词典。如果您在Firebug / Chrome开发工具中查看网络页面,您会看到您的网页向某些不存在的资源(favicion.ico?)发出请求 - 这会导致not_found_view成为调用,但由于异常,请求导致501响应而不是404。