這裏之前引用/ create_playlist /局部變量「播放列表」是我的views.py:UnboundLocalError在分配
def create_playlist(request):
form = PlaylistForm(request.POST or None)
if form.is_valid():
playlist = form.save(commit=False)
playlist.name = request.name
context={
'playlist':playlist,
'name':playlist_name,
}
return render(request, 'create_playlist.html', context)
playlist.save()
context = {
"form": form,
}
return render(request, 'create_playlist.html', {'form': form,})
我有Playlist
模型,並forms.py
其中包含了播放列表model.I的各個領域要用戶可以創建自己的播放列表,併爲我做了這個,但是當我編譯它,它給了我這個錯誤:
UnboundLocalError at /create_playlist/
local variable 'playlist' referenced before assignment
Request Method: GET
Request URL: http://localhost:8000/create_playlist/
Django Version: 1.9.6
Exception Type: UnboundLocalError
Exception Value:
local variable 'playlist' referenced before assignment
編輯:WSGIRequest」對象有沒有屬性‘名’
這裏是我的models.py
class Playlist(models.Model):
name = models.CharField(max_length=200, null=False, blank=False,default='')
songs = models.ManyToManyField('Song')
def __str__(self):
return self.name
這裏是我的forms.py:
class PlaylistForm(forms.ModelForm):
class Meta:
model=Playlist
fields = ['name', 'songs' ]
''playlist'範圍if'和你外面使用它。如果'if'語句不滿足,這個錯誤可能會發生 – kuro