Hi All,
I'm a recovering PHP programmer who is using Python to gain a deeper
understanding of programming and web technologies. I've been doing
the Python thing, in various different capacities, in my spare time.
All roads seem to lead to Werkzeug for my coding style/philosophy.
I'm having trouble with the redirect functionality in the dispatching
system. According to the documentation, the MapAdapter object will
redirect /foo to /foo/ if strict_slashes was set to true on the Map
object. Although I am able to get the system to raise a
RequestRedirect exception, I'm at a loss as to how to get the Response
object to send a Location: header to the browser in this situation.
My code raises an AttributeError: 'RequestRedirect' object has no
attribute 'find'
*** Begin code snippet. ***
# Standard imports
import sys, os, os.path
# Library Imports
from werkzeug import Request, Response, DebuggedApplication,
run_simple
from werkzeug.routing import Map, Rule, NotFound, RequestRedirect
from werkzeug.exceptions import HTTPException, NotFound
# Meta
debug = True
base_dir = '/home/swoods/personal/development'
sys.path.append(base_dir)
# Application-Level Imports
from wsgi_test_actions import Actions
# End preamble
url_map = Map([
Rule('/', endpoint='pyinfo'),
Rule('/<int:year>/', endpoint='year'),
Rule('/<int:year>', endpoint='year')])
def app(environ, start_response):
try:
request = Request(environ)
response = Response(mimetype='text/html')
actions = Actions(base_dir)
urls = url_map.bind_to_environ(environ)
try:
endpoint, args = urls.match()
endpoint_fn = getattr(actions, endpoint)
response.data = endpoint_fn(args)
except NotFound, e:
response.data = 'Not Found!'
except RequestRedirect, e:
response.location = e
return response(environ, start_response)
except HTTPException, e:
return e
if debug:
application = DebuggedApplication(app)
else:
application = app
if __name__ == '__main__':
run_simple('192.168.1.151', 4000, application, use_reloader=debug)
*** End code snippet. ***
I'm able to throw the exception via REPL also:
***
Python 2.6 (r26:66714, Nov 2 2008, 13:50:20)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from werkzeug.routing import Map, Rule
>>> url_map = Map([Rule('/', endpoint='index'),Rule('/<int:year>/',
>>> endpoint='year')])
>>> a = url_map.bind('127.0.0.1')
>>> a.match('/600/')
('year', {'year': 600})
>>> a.match('/600')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/swoods/_/lib/python2.6/site-packages/Werkzeug-0.4-
py2.6.egg/werkzeug/routing.py", line 1159, in match
path_info.lstrip('/')
werkzeug.routing.RequestRedirect: http://127.0.0.1/600/
***
I feel like this is basic functionality, sorry if I don't understand.
Thanks,
Sean
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---