我面對這個錯誤在Django的Python:我不斷收到在Django以下錯誤: 「UnboundLocalError在/ MyApp的/」
「UnboundLocalError在/ MyApp的/」 局部變量 '專輯' 分配
之前引用我創建的意見models.py文件並導入一個類,但面對這個錯誤
下面是這兩個文件的完整代碼:
Models.py
from django.db import models
from django.db import models
class album(models.Model):
artist = models.CharField(max_length=250)
title = models.CharField(max_length=500)
gender = models.CharField(max_length=100)
def __str__(self):
return self.artist+'--'+self.title
views.py
from django.http import HttpResponse
from .models import album
def myapp(request):
all_albums = album.objects.all()
title = album.artist
html = ''
for album in all_albums:
url = '/myapp/' + str(album.id) + '/'
html += '<a href="' + url + '">' + title + '</a><br>'
return HttpResponse(html)
您正在使用'album'變量多次。一次作爲模型,其他時間作爲一個實例。模型名稱理想情況下應該是CamelCased。 –
你能解釋一下如何在CamelCased中編寫模型代碼嗎? – habib
您只需將'class album(models.Model):'更改爲'class Album(models.Model):',如[我的答案]中所述(https://stackoverflow.com/questions/45320036/i-keep-getting -the跟隨誤差功能於Django的unboundlocalerror-AT-MYAPP#答案-45320209)。但單靠這一點無法解決問題。 –