2012-05-31 3 views
1

안녕하세요 저는 Regex와 항상 혼동되어 있었고 다른 도움말 스레드에 대한 응답을 이해하지 못합니다.Django URL 정규식이있는 TypeError

은 기본적으로 내 질문에 내가이에

r'^input/?$' 

r'^input/index.html?$' 

을 결합 할 수있다? 그래서 아마 그것은 정규식 문제가 아니라, 제대로 일치 할 때 그것은 단지 오류를 제공

input() takes exactly 1 argument (3 given) 

: 장고에서

r'^input(/(index.html?)?)?$' 

,이 오류가?

답변

1

개인적으로 두 정규식을 결합하지 않는 것이 좋습니다. 나는 두 개의 URL 패턴을 가지고 있다고 생각합니다.

url(r'^input/?$', input, name="input"), 
url(r'^input/index.html?$', input), 

보다 읽기 쉽습니다. 당신이 두 가지를 결합하려는 경우

그러나 비 캡처 괄호를 사용할 수 있습니다 도움이 될 수 있습니다

r'^input(?:/(?:index.html?)?)?$' 

빠른 예를 설명 :

>>> import re 
>>> # first try the regex with capturing parentheses 
>>> capturing=r'^input(/(index.html?)?)?$' 
>>> # Django passes the two matching strings to the input view, causing the type error 
>>> print re.match(capturing, "input/index.html").groups() 
('/index.html', 'index.html') 
>>> # repeat with non capturing parentheses 
>>> non_capturing=r'^input(?:/(?:index.html?)?)?$' 
>>> print re.match(non_capturing, "input/index.html").groups() 
() 

더의 Regular Expression Advanced Syntax Reference 페이지를 참조하십시오 정보.

0

urlpatterns에서 이것을 사용하는 경우에는 ? 기호를 쓸 필요가 없습니다. 그 이후에 오는 모든 것은보기 기능에서 구문 분석 할 수 있기 때문입니다. 그리고 /input/index.html?param=2에 대한 요청은 r'^input/index.html$' 정규식으로 올바르게 처리됩니다. 그런 다음 뷰 기능에서 당신과 같이 매개 변수를 얻을 수 있습니다

def my_view(request): 
    param = request.GET.get('param', 'default_value') 

여기에 더 많은 정보를 찾기 : https://docs.djangoproject.com/en/1.9/topics/http/urls/