2016-11-18 5 views
1

나는 간단한 TurboGears 2 스크립트라는 app.py 있습니다 : "안녕하세요 세계"나는 app.py을 실행하고 http://localhost:5000/all__things를 방문 할 때, 내가 볼TurboGears는 URL에서 어떤 문자를 대체합니까?

#!/usr/bin/env python3 

from wsgiref.simple_server import make_server 
from tg import expose, TGController, AppConfig 

class RootController(TGController): 
    @expose() 
    def all__things(self): 
     return "Hello world!" 

config = AppConfig(minimal=True, root_controller=RootController()) 

print("Serving on port 5000...") 
httpd = make_server('', 5000, config.make_wsgi_app()) 
httpd.serve_forever() 

을 예상대로 그러나 이러한 URL도 작동 :

http://localhost:5000/all--things 
http://localhost:5000/[email protected]@things 
http://localhost:5000/all$$things 
http://localhost:5000/all++things 
http://localhost:5000/all..things 
http://localhost:5000/all,,things 

을뿐만 아니라 조합과 같이

http://localhost:5000/all-_things 
http://localhost:5000/all_-things 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected]$things 

기타 등등 ...

TurboGears에 밑줄을 대체 할 수있는 문자의 전체 목록은 무엇

URL?

또한이 기능을 일부 문자 만 대체하도록 제한 할 수 있습니까? 이상적으로는 대시 (http://localhost:5000/all--things)가있는 URL이 작동하고 밑줄이있는 URL (http://localhost:5000/all__things) 또는 다른 이상한 문자가 작동하지 않는 것이 좋습니다.

답변

1

path_translator에 의해 관리되며 의 dispatch_path_translator 옵션을 통해 구성 할 수 있습니다. None을 전달하거나 사용자 지정 함수를 제공하여 비활성화 할 수 있습니다.

제공된 모든 기능은 현재 처리중인 경로의 일부를 수신하고 정규화 된 상태로 반환해야합니다.

이 기본 경로 번역기가 string.punctuation을 기반으로 당신이 @route 장식을 통해 좀 더 복잡한 경우에 당신을 도울 수있는 당신이 https://github.com/TurboGears/tgext.routes을 고려 제안 정의 라우팅 요구 사항이 경우

(https://github.com/python/cpython/blob/c30098c8c6014f3340a369a31df9c74bdbacc269/Lib/string.py#L31 참조).

+0

'config.dispatch_path_translator = False'를 설정하면 프로그램이 충돌하지만'config.dispatch_path_translator = None'이 작동합니다. 결국 나는 다음과 같이 결정했다. 'config.dispatch_path_translator = lambda path_piece : path_piece.replace ('-', '_') path_piece에 '_'이 없으면 else '' ' 도움을 주셔서 감사합니다. –

+0

아, 죄송합니다. 참/없음/기능입니다. 반사 (True)의 반대로 False를 썼습니다 : D 답변이 업데이트되었습니다. – amol