我有一個項目,該代碼使用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'
你應該與錯誤一起,得到一個堆棧跟蹤,告訴你什麼行這種情況正在發生。它告訴你什麼? – Shadow
我是新來的python:/你能告訴我如何得到這個堆棧跟蹤? –
如果您正在運行django網絡服務器,它應該在黃色錯誤頁面上。只要找到它開始提到你的python文件的頁面部分。 – Shadow