【问题标题】:Error in Pyramid todo web appPyramid todo Web 应用程序中的错误
【发布时间】:2014-03-24 13:24:13
【问题描述】:

我一直在学习金字塔待办事项列表教程:

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

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

【问题讨论】:

    标签: python pyramid


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多