我在 django 中不断收到以下错误:"UnboundLocalError at /myapp/"
I keep getting the following error in django: "UnboundLocalError at /myapp/"
我在 django 中遇到这个错误 python:
"UnboundLocalError at /myapp/"
赋值前引用的局部变量'album'
我在 models.py 文件中创建了一个 class 并在视图中导入但遇到此错误
这是两个文件的完整代码:
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)
像这样改变视图,
def myapp(request):
all_albums = album.objects.all()
html = ''
for album in all_albums:
url = '/myapp/' + str(album.id) + '/'
html += '<a href="' + url + '">' + album.artist + '</a><br>'
return HttpResponse(html)
将标题移到循环内并更好地使用与模型不同的循环变量名称
html = ''
for album_data in all_albums:
url = '/myapp/' + str(album_data.id) + '/'
title = album_data.artist
html += '<a href="' + url + '">' + title + '</a><br>'
return HttpResponse(html)
您正在为多个变量使用 album
名称,一次作为模型,其他时候作为实例。理想情况下,型号名称应采用 CamelCased。更正模型名称后,将 title
变量赋值移动到 for
循环内。只有后者才能暂时解决您的问题,但如果您坚持样式指南 (PEP-8),您以后就不会遇到此类问题。
models.py
...
class Album(models.Model):
artist = models.CharField(max_length=250)
...
views.py
...
from .models import Album
def myapp(request):
all_albums = Album.objects.all()
html = ''
for album in all_albums:
title = album.artist
url = '/myapp/' + str(album.id) + '/'
html += '<a href="' + url + '">' + title + '</a><br>'
...
完全复制并粘贴到您的视图中
from django.http import HttpResponse
from .models import album
def myapp(request):
all_albums = album.objects.all()
html = ''
for al in all_albums:
url = '/myapp/' + str(al.id) + '/'
html += '<a href="' + url + '">' + al.artist + '</a><br>'
return HttpResponse(html)
我在 django 中遇到这个错误 python:
"UnboundLocalError at /myapp/" 赋值前引用的局部变量'album'
我在 models.py 文件中创建了一个 class 并在视图中导入但遇到此错误
这是两个文件的完整代码:
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)
像这样改变视图,
def myapp(request):
all_albums = album.objects.all()
html = ''
for album in all_albums:
url = '/myapp/' + str(album.id) + '/'
html += '<a href="' + url + '">' + album.artist + '</a><br>'
return HttpResponse(html)
将标题移到循环内并更好地使用与模型不同的循环变量名称
html = ''
for album_data in all_albums:
url = '/myapp/' + str(album_data.id) + '/'
title = album_data.artist
html += '<a href="' + url + '">' + title + '</a><br>'
return HttpResponse(html)
您正在为多个变量使用 album
名称,一次作为模型,其他时候作为实例。理想情况下,型号名称应采用 CamelCased。更正模型名称后,将 title
变量赋值移动到 for
循环内。只有后者才能暂时解决您的问题,但如果您坚持样式指南 (PEP-8),您以后就不会遇到此类问题。
models.py
...
class Album(models.Model):
artist = models.CharField(max_length=250)
...
views.py
...
from .models import Album
def myapp(request):
all_albums = Album.objects.all()
html = ''
for album in all_albums:
title = album.artist
url = '/myapp/' + str(album.id) + '/'
html += '<a href="' + url + '">' + title + '</a><br>'
...
完全复制并粘贴到您的视图中
from django.http import HttpResponse
from .models import album
def myapp(request):
all_albums = album.objects.all()
html = ''
for al in all_albums:
url = '/myapp/' + str(al.id) + '/'
html += '<a href="' + url + '">' + al.artist + '</a><br>'
return HttpResponse(html)