2010-04-17 4 views
4

적절한 링크를 클릭하면 모든 아티스트, 앨범 및 노래를 나열하는 webapp가 있습니다. 일반 뷰 (object_list/detail)와 명명 된 URL을 광범위하게 사용하지만 성가신 점을 발견하게됩니다.Django : 명명 된 URL/동일한 템플릿, 다른 명명 된 URL

{% extends "base.html" %} 
{% block content %} 
<div id="content"> 

<ul id="starts-with"> 
{% for starts_with in starts_with_list %} 
    <li><a href="{% url song_list_x starts_with %}">{{ starts_with|upper }}</a></li> 
{% endfor %} 
</ul> 

<ul> 
{% for song in songs_list %} 
    <li>{{ song.title }}</li> 
{% endfor %} 
</ul> 

</div> 
{% endblock content %} 

내 아티스트 및 앨범 템플릿이 거의 같은 모양과 내가 하나에 세 가지 템플릿의 결합 싶습니다 나는 3 템플릿 꽤 많이 출력 그냥 다음과 같을 동일한 HTML을 가지고있다. 내 변수가 song으로 시작한다는 사실은 쉽게 기본값 obj으로 변경할 수 있습니다. 그것은 내 <ul id="starts-with"> URL을 어떻게 수정 해야할지 모르겠다. 분명히 내 urls.py에 명명 된 URL을 사용하여 특정 앨범/아티스트/노래에 연결하고 싶지만 컨텍스트 인식 방법을 모르겠습니다. 어떤 제안?

urls.py : 당신은 아티스트, 앨범 및 노래에 대해 개별적으로 일반적인 OBJECT_TYPE에 대한 URL 패턴을 정의 대신 할 수

urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views', 
    # (r'^$', index), 
    url(r'^artists/$', artist_list, name='artist_list'), 
    url(r'^artists/(?P<starts_with>\w)/$', artist_list, name='artist_list_x'), 
    url(r'^artist/(?P<artist_id>\d+)/$', artist_detail, name='artist_detail'), 
    url(r'^albums/$', album_list, name='album_list'), 
    url(r'^albums/(?P<starts_with>\w)/$', album_list, name='album_list_x'), 
    url(r'^album/(?P<album_id>\w)/$', album_detail, name='album_detail'), 
    url(r'^songs/$', song_list, name='song_list'), 
    url(r'^songs/(?P<starts_with>\w)/$', song_list, name='song_list_x'), 
    url(r'^song/(?P<song_id>\w)/$', song_detail, name='song_detail'), 
) 

답변

3

: 템플릿에서 다음

urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views', 
    # (r'^$', index), 
    url(r'^(?P<object_type>\w+)/$', music_object_list, name='music_object_list'), 
    url(r'^(?P<object_type>\w+)/(?P<starts_with>\w)/$', music_object_list, name='music_object_list_x'), 
    url(r'^(?P<object_type>\w+)/(?P<object_id>\d+)/$', music_object_detail, name='music_object_detail'), 

) 

, 당신의 URL 태그가된다

{% url music_object_list_x object_type starts_with %} * 

하나의보기, music_object_list 만 필요할 수 있습니다. 각 개체 유형에 대해 다른 기능이 필요한 경우 music_object_list의 개별 기능을 호출하십시오.

def music_object_list(request, object_type, starts_with=None): 
    if object_type == 'artists': 
     return artist_list(request, starts_with=starts_with) 
    elif object_type == 'albums': 
     return album_list(request, starts_with=starts_with) 
    ... 

당신이 django.views.generic.list_detail.object_list를 사용하는 경우, 다음 extra_context 사전에 object_type을 추가해야합니다. 이렇게하면 url 태그가 작동 할 수 있도록 object_type이 템플리트 컨텍스트에 추가됩니다.

extra_context = {'object_type': 'songs', ...} 

*이 장고 1.2 새 URL 태그 구문이다. 이전 버전의 경우 쉼표를 사용합니다.

{% url music_object_list_x object_type,starts_with %} 

이가 어디로 가는지 내가 좋아하는 자세한 내용

+1

에 대한 문서 (Current, 1.1)를 참조하십시오! 나는 우리가 말하는 것처럼 이것이 어떻게 작동 하는지를보고자 노력하고있다. 이것이 계획대로 진행된다면 나는 영원히 감사 할 것입니다. 내가 바꿀 유일한 것은'object_list'를 함수 이름으로 사용하는 것이'from django.views.generic.list_detail import object_list'와 충돌하는 것 같지만 쉽게 극복 할 수 있습니다. 내 발견으로 돌아올거야! – TheLizardKing

+0

이름 충돌에 대한 좋은 지적. 함수의 이름을'music_object_list'로 바꿨습니다. – Alasdair

+0

내 템플릿의 object_type을 인식하기 위해'{% url music_object_list_x object_type, starts_with %} '을 (를) 얻는데 어려움을 겪고 있다고 생각합니다. 나는 장고에있다 1.1.1 – TheLizardKing