Django NameError - 名称' s'未定义

时间:2018-02-04 14:56:49

标签: python ajax django list view

业余开发者, 我有以下代码在Python 2.7,Django 1.10中运行良好。但是现在当我升级到Django 2+和Python 3+时,它会出错。

查看代码:

def get_class_name(request):
    class_name = ClassName.objects.get(pk = request.GET.get('class_name', None))
    classes = Class.objects.filter(class_name = class_name)
    l = [[", ".join([s.subject for s in x.subject.all()]),s.id] for x in classes]

错误消息: / ajax / get_class_name /中的NameError 名字' s'没有定义 请求方法:GET 请求网址:http://localhost:8000/ajax/get_class_name/?class_name=1 Django版本:2.0 异常类型:NameError 例外价值:
名字' s'未定义

型号:     class Class(models.Model):         subject = models.ManyToManyField(Subject)

我该如何解决?

2 个答案:

答案 0 :(得分:0)

def get_class_name(request, pk):
    pk = request.GET.get('class_name', None)
    class_name = ClassName.objects.get(pk = pk)
    classes = Class.objects.filter(class_name = class_name)
    l = [[", ".join([s.subject for s in x.subject.all()]),s.id] for x in classes]

答案 1 :(得分:0)

def get_class_name(request):
    class_name = ClassName.objects.get(pk = request.GET.get('class_name', None))
    classes = Class.objects.filter(class_name = class_name)
    l = [[", ".join([s.subject for s in x.subject.all()]),x.id] for x in classes]

我认为问题是,s.id] for x s在第一个列表理解之外,可能是你的意思,x.id] for x in classes]