【问题标题】:I keep getting the following error in django: "UnboundLocalError at /myapp/"我在 django 中不断收到以下错误:“UnboundLocalError at /myapp/”
【发布时间】:2017-07-26 07:19:08
【问题描述】:

我在 django python 中遇到了这个错误:

“/myapp/ 处的 UnboundLocalError” 赋值前引用的局部变量“专辑”

我在 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 编写模型代码吗?
  • 您只需将class album(models.Model): 更改为class Album(models.Model):,如my answer 中所述。但这并不能解决问题。

标签: python django local-variables


【解决方案1】:

在循环内移动标题并更好地使用循环变量名而不是模型

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)

【讨论】:

  • 同样的错误?如果不显示,可能会很好显示完整的新代码
【解决方案2】:

像这样改变视图,

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)

【讨论】:

    【解决方案3】:

    完全复制并粘贴到您的视图中

    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)
    

    【讨论】:

      【解决方案4】:

      您将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>'
      ...
      

      【讨论】:

      • @habib 你需要发布一个完整的堆栈跟踪。此外,如果您使用建议的更改更新了代码,请编辑您的问题以添加更新的代码。
      猜你喜欢
      • 1970-01-01
      • 2021-02-02
      • 2021-04-23
      • 2020-06-20
      • 1970-01-01
      • 2019-08-20
      • 2020-11-29
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多