Django order_by('?')[:n] n 使用我从数据库中获取的变量?但不工作
Django order_by('?')[:n] n using a variable which I take from the database? but not work
def home (request):
questions = Article.objects.order_by('?')
n = guestionNum.objects.all()
n = n[0]
answers = Answer.objects.all().order_by('?')[:n]
return render_to_response('question/home.html', {'questions': questions, 'answers': answers,'n':n, 'username':auth.get_user(request).username}) `
TypeError:'guestionNum' 和 'int'
实例之间不支持“>=”
如何使用从数据库中获取的变量 n 制作切片?
n = guestionNum.objects.all()
n = n[0]
你不能切片因为上面的行 returns 一个实例不是 int 对象
尝试
n = guestionNum.objects.all()
n=n[0].fieldname
然后传给切片
n = guestionNum.objects.all()
n = n[0]
# n == guestionNum.objects.first() // Both have same type and data
现在 n
的类型是实例而不是查询集。
因此您可以使用下面代码中的直接 n 值。
n = guestionNum.objects.first().fieldname
def home (request):
questions = Article.objects.order_by('?')
n = guestionNum.objects.all()
n = n[0]
answers = Answer.objects.all().order_by('?')[:n]
return render_to_response('question/home.html', {'questions': questions, 'answers': answers,'n':n, 'username':auth.get_user(request).username}) `
TypeError:'guestionNum' 和 'int'
实例之间不支持“>=”如何使用从数据库中获取的变量 n 制作切片?
n = guestionNum.objects.all()
n = n[0]
你不能切片因为上面的行 returns 一个实例不是 int 对象
尝试
n = guestionNum.objects.all()
n=n[0].fieldname
然后传给切片
n = guestionNum.objects.all()
n = n[0]
# n == guestionNum.objects.first() // Both have same type and data
现在 n
的类型是实例而不是查询集。
因此您可以使用下面代码中的直接 n 值。
n = guestionNum.objects.first().fieldname