2017-10-10 170 views
0

我有一個項目,該代碼使用Django:Django的:對象有沒有屬性_set」

def all_songs(request, filter_by): 
    if not request.user.is_authenticated(): 
     return render(request, 'music/login.html') 
    else: 
     try: 
      song_ids = [] 
      for album in Album.objects.all(): 
       for song in album.song_set.all(): 
        song_ids.append(song.pk) 
      users_songs = Song.objects.filter(pk__in=song_ids) 
      for album in GenericAlbum.objects.all(): 
       for song in album.genericsong_set.all(): 
        song_ids.append(song.pk) 
      generic_songs = GenericSong.objects.filter(pk__in=song_ids) 
     except Album.DoesNotExist: 
      users_songs = [] 
     except GenericAlbum.DoesNotExist: 
      generic_songs = [] 
     return render(request, 'music/songs.html', { 
      'song_list_all': users_songs, 
      'generic_song_list': generic_songs, 
      'filter_by': filter_by, 
     }) 

當我去allSong頁面,我得到這個錯誤:

AttributeError: 'NoneType' object has no attribute 'split'

我想一個變量允許一個None值,但我不知道巫婆之一,我試過了他們所有的問題。

這是回溯:

Traceback (most recent call last): 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 639, in process_request_thread 
    self.finish_request(request, client_address) 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 361, in finish_request 
    self.RequestHandlerClass(request, client_address, self) 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\servers\basehttp.py", line 99, in __init__ 
    super(WSGIRequestHandler, self).__init__(*args, **kwargs) 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 696, in __init__ 
    self.handle() 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle 
    handler.run(self.server.get_app()) 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 144, in run 
    self.close() 
    File "C:\Users\elmou\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\simple_server.py", line 35, in close 
    self.status.split(' ',1)[0], self.bytes_sent 
AttributeError: 'NoneType' object has no attribute 'split' 
+0

你應該與錯誤一起,得到一個堆棧跟蹤,告訴你什麼行這種情況正在發生。它告訴你什麼? – Shadow

+0

我是新來的python:/你能告訴我如何得到這個堆棧跟蹤? –

+0

如果您正在運行django網絡服務器,它應該在黃色錯誤頁面上。只要找到它開始提到你的python文件的頁面部分。 – Shadow

回答

1

試試吧,你嘗試從queryset對象獲取pk,但PK吧是實例的屬性不queryset的,當你從需要值列表查詢集可以使用values-list,並添加默認爲您filter_by

def all_songs(request, filter_by=None): 
    if not request.user.is_authenticated(): 
     return render(request, 'music/login.html') 
    else: 
     song_ids = [] 
     for album in Album.objects.all(): 
      song_ids.append(album.song_set.values_list('pk', flat=True)) 
     users_songs = Song.objects.filter(pk__in=song_ids) 
     song_ids = [] 
     for album in GenericAlbum.objects.all(): 
      song_ids.append(album.genericsong_set.values_list('pk', flat=True)) 
     generic_songs = GenericSong.objects.filter(pk__in=song_ids) 

     return render(request, 'music/songs.html', { 
      'song_list_all': users_songs, 
      'generic_song_list': generic_songs, 
      'filter_by': filter_by, 
     }) 
+0

它沒有工作:/ –

+0

你如何運行django? –

+0

我乳寧的Django在pycharm親,Django的版本1.9.4 –

相關問題