적절한 링크를 클릭하면 모든 아티스트, 앨범 및 노래를 나열하는 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'),
)
에 대한 문서 (Current, 1.1)를 참조하십시오! 나는 우리가 말하는 것처럼 이것이 어떻게 작동 하는지를보고자 노력하고있다. 이것이 계획대로 진행된다면 나는 영원히 감사 할 것입니다. 내가 바꿀 유일한 것은'object_list'를 함수 이름으로 사용하는 것이'from django.views.generic.list_detail import object_list'와 충돌하는 것 같지만 쉽게 극복 할 수 있습니다. 내 발견으로 돌아올거야! – TheLizardKing
이름 충돌에 대한 좋은 지적. 함수의 이름을'music_object_list'로 바꿨습니다. – Alasdair
내 템플릿의 object_type을 인식하기 위해'{% url music_object_list_x object_type, starts_with %} '을 (를) 얻는데 어려움을 겪고 있다고 생각합니다. 나는 장고에있다 1.1.1 – TheLizardKing