2009-10-25 3 views
10

장고에서 SO가 사용하는 형태의 장황한 URL을 사용하려고합니다 : example.com/id/slug. 슬러그를 활성화하는 데 아무런 문제가 없으며 URL은 현재 http://127.0.0.1:8000/articles/id/ (예 :/articles/1 /) 형식으로 설정되어 있으며 정상적으로 작동합니다. 해당 URLPATTERN은 다음과 같습니다Django에서 부적절한 URL을 활성화하십시오.

(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 

난에 URL 패턴을 변경하는 경우 :

내가 http://127.0.0.1:8000/articles/another-article/로 이동하면
(r'^(?P<slug>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 

가 그럼 난 다음 eror을받을 :

The current URL, articles/another-article/, didn't match any of these.

경우에는, 그러나, 시도 :

http://127.0.0.1:8000/articles/1/ 

나는 오류 얻을 : 내가 통해 aricle를 탐색 할 수 있도록하려면 궁극적으로

No article found matching the query

을 중 하나를

http://127.0.0.1:8000/articles/1/ or http://127.0.0.1:8000/articles/1/another-article/

답변

25

그래야 내가 생각하기 때문에이 질문을하기 전에하고 조금 더 환자가 응답 아웃 :

(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 
(r'^(?P<object_id>\d+)/(?P<slug>[-\w]+)/$', 'django.views.generic.list_detail.object_detail', info_dict), 

제 1 패턴은 형태의 URL을 허용/기사 제 URLPATTERN는 (슬러그를 포함)을 의미/1/선택적이다.

+0

예, 실제 슬러그에 대한 정규식 패턴 일치가 누락되었습니다. –

관련 문제