RESTful routing在Python中似乎不起作用。例如,RESTful Python路由 - 不工作?
# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect("add user", "/user", controller = "addUser", action = "add",
conditions=dict(method=["GET"]))
map.connect("post user", "/user", controller = "postUser", action = "post",
conditions=dict(method=["POST"]))
map.connect("update user", "/user/{id}", controller = "updateUser", action = "update",
conditions=dict(method=["GET"]))
map.connect("put user", "/user/{id}", controller = "putUser", action = "put",
conditions=dict(method=["PUT"]))
map.connect("delete user", "/user/{id}", controller = "deleteUser", action = "delete",
conditions=dict(method=["DELETE"]))
map.connect("home", "/", controller = "main", action = "index", conditions=dict(method=["GET"]))
# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):
# Get the request uri.
uri = environ.get('PATH_INFO', '')
req = uri if uri else '/'
# Match a URL, returns a dict or None if no match
mapped = map.match(req)
print mapped
# Everything done, return the response:
if mapped['action'] == 'index' :
response_body = index(environ, start_response, mapped)
elif mapped['action'] == 'add':
response_body = add(environ, start_response, mapped)
elif mapped['action'] == 'put':
response_body = put(environ, start_response, mapped)
elif mapped['action'] == 'delete':
response_body = delete(environ, start_response, mapped)
else:
response_body = "Not exist."
status = '200 OK'
response_headers = [('Content-Type', 'text/html'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
它不關心的REST方法是否PUT,DELETE或POST。它只在我的網址尋找匹配的/user/{id}
。
所以,當我發DELETE法http://127.0.0.1/index.py/user/1
我總是{'action': u'update', 'controller': u'updateUser', 'id': u'1'}
這是GET從map.connect("update user", "/user/{id}", controller = "updateUser", action = "update", conditions=dict(method=["GET"]))
任何想法,我做了什麼錯?
編輯
Apache的配置文件,
LoadModule wsgi_module modules/mod_wsgi.so
....
<VirtualHost 127.0.0.1>
DocumentRoot "${path}/data/localweb"
ServerName 127.0.0.1
<Directory "${path}/data/localweb">
Options FollowSymLinks Indexes ExecCGI
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
WSGIScriptAlias/C:\...\wsgi\route\basic\index.py
</VirtualHost>
順便說一句,在我的Apache的錯誤日誌,我總是得到這個wsgi:error
與打印結果的警告,
[星期六八月15'16:03:41.871541 2015] [wsgi:error] [pid 7068:tid 836] {'action':u'update','controller':u'updateUser','id':u'1'}
「http://127.0.0.1/index.py/user/1」不等於「http://127.0.0.1/user/1」。 你在使用錯誤的索引。 .py的目錄索引(您希望它的目錄)。 Django也保持着類似的錯誤。該URL不能是標籤中的腳本名稱。否則,該地區將會改變。 – dsgdfg
「index.py''的外觀可能是如何設置mod_wsgi的假象。具體來說,可以在目錄上使用WSGIScriptAlias而不是特定的WSGI腳本文件,或者使用設置mod_wsgi的「AddHandler」方法。應用程序的''SCRIPT_NAME''或掛載點通常應該設置正確,''PATH_INFO''應該只包含'/ user/...',WSGI應用程序應該很好地匹配。 OP應該提供他們的Apache/mod_wsgi配置。 –
@SDilmac它與'http:// 127.0.0.1/user/1'是'{'action':u'update','controller':u'updateUser','id':u' 1'}'。 – laukok