1

pyramid/url.py 似乎有一个错误。这是代码

import os
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
here = os.path.dirname(os.path.abspath(__file__))

@view_config(route_name='firstview',renderer='firstview.mako',request_method='GET')
def firstview(request):
    return {'example':'text'}

@view_config(route_name='secondview',request_method='POST')
def secondview(request):
    return Response('second view')

if __name__ == '__main__':
    settings = {}
    settings['mako.directories'] = os.path.join(here,'templates')
    config = Configurator(settings=settings)
    config.add_route('firstview','/firstview')
    config.add_route('secondview','/secondview')
    config.scan()
    app = config.make_wsgi_app()

这是 firstview 的模板

# -*- coding: utf-8 -*- 
<form name="example"
action="${request.route_url('secondview')}"
method="POST">
  example
    <textarea rows="4" cols="50" name="name">
    </textarea><br>
    <input type="submit" value="Submit">
</form>

这是堆栈跟踪

2013-06-10 21:05:40,280 - DEBUG - http://localhost:8080 #app_url
2013-06-10 21:05:40,280 - DEBUG - None #path
2013-06-10 21:05:40,280 - DEBUG -  #suffix
2013-06-10 21:05:40,280 - DEBUG -  #qs
2013-06-10 21:05:40,280 - DEBUG -  #anchorTraceback (most recent call last):
  File "/Users/abc/env/bin/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/router.py", line 251, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/router.py", line 227, in invoke_subrequest    response = handle_request(request)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
    response = handler(request)  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/config/views.py", line 321, in attr_view    return view(context, request)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/config/views.py", line 297, in predicate_wrapper
    return view(context, request)  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/config/views.py", line 369, in rendered_view
    context)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/renderers.py", line 531, in render_view    return self.render_to_resp
onse(response, system, request=request)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/renderers.py", line 561, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/renderers.py", line 557, in render
    result = renderer(value, system_values)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/mako_templating.py", line 215, in __call__    
    reraise(MakoRenderingException(errtext), None, exc_info[2])
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/mako_templating.py", line 207, in __call__
    result = template.render_unicode(**system)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/template.py", line 452, in render_unicode
    as_unicode=True)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 783, in _render 
    **_kwargs_for_callable(callable_, data))
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 815, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 841, in _exec_template
    callable_(context, *args, **kwargs)
  File "firstview_mako", line 23, in render_body
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/url.py", line 267, in route_url
    return app_url + path + suffix + qs + anchor
MakoRenderingException:

Traceback (most recent call last):
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/mako_templating.py", line 207, in __call__
    result = template.render_unicode(**system)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/template.py", line 452, in render_unicode
    as_unicode=True)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 783, in _render
    **_kwargs_for_callable(callable_, data))
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 815, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/Users/abc/env/bin/lib/python2.7/site-packages/Mako-0.8.0-py2.7.egg/mako/runtime.py", line 841, in _exec_template
    callable_(context, *args, **kwargs)
  File "/Users/abc/piramide/bugreplica/templates/firstview.mako", line 3, in render_body
    action="${request.route_url('secondview')}"
  File "/Users/abc/env/bin/lib/python2.7/site-packages/pyramid-1.4.1-py2.7.egg/pyramid/url.py", line 267, in route_url
    return app_url + path + suffix + qs + anchor
TypeError: cannot concatenate 'str' and 'NoneType' objects

由于我添加了一些用于调试目的的行,因此行号会有所不同。

无论如何,我注意到pathreturn None。在代码中没有处理这种情况。我是金字塔框架的新手,非常感谢您的帮助。谢谢!

附言

显然这个问题源于 url.py 的函数 route_url。专门route.pregenerator(self, elements, kw)退货None,没有处理这种情况。

4

0 回答 0