2016-11-02 10 views
0

그래서 데이터베이스의 특정 아티스트가 발표 한 제목과 노래 및 앨범을 표시하는 프로젝트를 진행하고 있습니다. 나는 좀 붙어있다. 앨범 링크를 클릭하면 앨범에있는 노래를 표시하는 페이지를 만들려고합니다. 링크를 클릭하면 URL에 아티스트 이름과 앨범 제목이 표시되지만 동일한 오류가 계속 발생합니다. 앨범 이름과 아티스트 이름이 일치합니다.Django의 URL conf와 관련된 문제

페이지에 앨범의 노래를 표시하려면 어떻게해야합니까?

Money

여기 뷰에 대한 코드의

def home(request): 
    artistes = Artiste.objects.all() 
    return render(request, 'music/home.html', locals()) 


def details(request, artiste): 
    artistes = get_object_or_404(Artiste, name=artiste) 
    album = Artiste.objects.get(name=artiste).album_set.all() 
    single = Artiste.objects.get(name=artiste).song_set.filter(single=True) 
    return render(request, 'music/details.html', {'artistes': artistes, 'single': single, 'album':album}) 


def album_details(request,name,album): 
    albums = get_object_or_404(Album, title=album) 
    artistes = get_object_or_404(Artiste, name=name) 
    single = Album.objects.get(title=album).song_set.filter(single=False) 
    return render(request, 'music/album_detail.html', {'albums': albums, 'single': single}) 

URL 패턴

app_name = 'music' 

urlpatterns = [ 
    url(r'^$', views.home, name='home'), 
    url(r'^(?P<artiste>.+)/$', views.details, name='details'), 
    url(r'^(?P<name>.+)/(?P<album>.+)/$', views.album_details, name='album_details') 
] 

앨범 세부 정보 페이지

<body> 
    {% if artistes.album_set.all %} 
     {% for songs in album %} 
      <h4>Albums</h4> 
      <!--<img src="{{album.art}}"><br> 
      --> 
      <a href="{% url 'music:album_details' songs.artiste songs.title %}">{{songs.title}}</a> 
      <h5>{{songs.artiste}}</h5> 
      <h5>{{songs.title}}</h5> 
     {% endfor %} 
    {% else %} 
     <h3>No albums available</h3> 
     <h5>Check Out Songs </h5> 
     <ul> 
     {% for song in artistes.song_set.all %} 
      <li>{{song}} - mp3</li> 
     {% endfor %} 
     </ul> 
    {% endif %} 


</body> 
</html> 
+0

오류는 URL에없는 albums = get_object_or_404 (앨범, 제목 = 앨범)입니다. – Zartch

+0

@Zartch 어떻게 수정합니까? – HackAfro

답변

0

URL이 너무 광범위합니다. .+과 일치합니다. (예 : album_details URL에 걸려는 'Wizked/Starboy'등)

당신은 그 URL 패턴의 순서를 전환하여이 문제를 해결할 수 있지만, 당신은 또한 그들이 덜 일반적인해야한다 - 예를 들면 r'^(?P<artiste>\w+)

+0

이것도 효과가 있습니다 – HackAfro

0

코드 확인을 보인다. 논리적 인 문제입니다.

오류는 다음과 같습니다.이 쿼리를 처리하는 예술가가 없습니다. 한 URL에

는 검색된 URL은 다음

URL (R '?.^(P < 아티스트> +)/$'views.details 이름 = '상세')

왜? ". +는"

albums = get_object_or_404(Album, title=album) 

앨범은에 "위즈 키드/Starboy을"동일

쿼리가 제기 렸기 때문에는 "위즈 키드/Starboy"

이 코드에 도착을 포함한 모든 일치 404 항목을 찾을 수 없으므로 404.

당신이 URL에 변경하면 "\의 w +"다니엘은 말했다 경기가 될 것와 "+."?.

URL (R '^ (P < 이름> +)/(? P < 앨범>.)/$ ', 전망.album_details, 이름은 = 'album_details')

그런 다음 PARAMS은 다음과 같습니다

이름 = 위즈 키드

앨범 = Starboy

그런 다음 코드를 실행합니다

albums = get_object_or_404(Album, title=album) 
artistes = get_object_or_404(Artiste, name=name) 

해당 이름의 앨범 및 아티스트와 함께 데이터베이스에 레코드가있는 경우 페이지가 표시됩니다.

albums = Album.objects.filter(title=album) 

는 HTML에서 당신이 할 수 있습니다 :

{% for songs in album %} 
      <h4>Albums</h4> 
      <!--<img src="{{album.art}}"><br> 
      --> 
      <a href="{% url 'music:album_details' songs.artiste songs.title %}">{{songs.title}}</a> 
      <h5>{{songs.artiste}}</h5> 
      <h5>{{songs.title}}</h5> 
     {% empty %} 
      No album was found 
     {% endfor %} 

에게 당신이 할 수있는이 방법을 당신은 여전히 ​​당신이 변경 제언 (404)

적어도 앨범을 조회하지인가 앨범이없는 아티스트의 페이지가 있습니다.

+0

모든 영웅들이 망토를 쓰는 것은 아닙니다. 당신은 영웅입니다. 감사. 나는이 일에 많은 시간을 할애했다. 축복 – HackAfro

+0

나는 영웅적인 단어 대신 정답을 선호한다. 하지만 그 사람. 천만에요. 코딩을 유지하십시오. – Zartch