Django索引超出范围

时间:2018-07-31 13:15:03

标签: python django list dictionary

代码改进问题: 自从两周以来,我一直在改进下面的代码,并且能够编写下面的代码,但是我仍然遇到问题,无法按预期工作。

有两个主要问题

  1. 我确实喜欢这样(j_list = str(hide [1]))我想设置一个声明 相反,我得到的是值1不能解决我的问题。
  2. 上下文[j_list [i]] = j_list [i]时索引超出范围错误

    context = {
            'instance': project,
            'user': user,
        }
        hide = [0,1]
        for i in range(10):
            j_list = "hide" + str(i)
            fp_list ="fp_list_" + str(i)
            j_list = str(hide[1])
            context[j_list[i]] = j_list[i]
            messages.add_message(request, messages.INFO, j_list)
            messages.add_message(request, messages.INFO, fp_list)
            try:
                fp_list[i] = FP.objects.filter(id__in=group[i][1])
                context[fp_list[i]] = fp_list[i]
                j_list[i] = hide[0]
            except IndexError:
                fp_list[i] == "null"
            return render(request, 'projects_detail.html', context)
    

旧的工作代码,但是它太难看了,我正在按照上面的代码尝试改善自己:

        hide0=1
        hide1=1
        hide2=1
        hide3=1
        hide4=1
        hide5=1
        hide6=1
        hide7=1
        hide8=1
        hide9=1
        try:
            fp_list_0 = FP.objects.filter(id__in=group[0][1])
            hide0 = 0
        except IndexError:
            fp_list_0 = "null"
        try:
            fp_list_1 = FP.objects.filter(id__in=group[1][1])
            hide1 = 0
        except IndexError:
            fp_list_1 = "null"
        try:
            fp_list_2 = FP.objects.filter(id__in=group[2][1])
            hide2 = 0
        except IndexError:
            fp_list_2 = "null"
        try:
            fp_list_3 = FP.objects.filter(id__in=group[3][1])
            hide3 = 0
        except IndexError:
            fp_list_3 = "null"
        try:
            fp_list_4 = FP.objects.filter(id__in=group[4][1])
            hide4 = 0
        except IndexError:
            fp_list_4 = "null"
        try:
            fp_list_5 = FP.objects.filter(id__in=group[5][1])
            hide5 = 0
        except IndexError:
            fp_list_5 = "null"
        try:
            fp_list_6 = FP.objects.filter(id__in=group[6][1])
            hide6 = 0
        except IndexError:
            fp_list_6 = "null"
        try:
            fp_list_7 = FP.objects.filter(id__in=group[7][1])
            hide7 = 0
        except IndexError:
            fp_list_7 = "null"
        try:
            fp_list_8 = FP.objects.filter(id__in=group[8][1])
            hide8 = 0
        except IndexError:
            fp_list_8 = "null"
        try:
            fp_list_9 = FP.objects.filter(id__in=group[9][1])
            hide9 = 0
        except IndexError:
            fp_list_9 = "null"
        context = {
            'instance': project,
            'user': user,
            "fp_list_0": fp_list_0,"fp_list_1": fp_list_1,"fp_list_2": fp_list_2,
            "fp_list_3": fp_list_3,"fp_list_4": fp_list_4,"fp_list_5": fp_list_5,
            "fp_list_6": fp_list_6,"fp_list_7": fp_list_7,"fp_list_8": fp_list_8,
            "fp_list_9": fp_list_9,
            "hide0": hide0,"hide1": hide1,"hide2": hide2,"hide3": hide3,"hide4": hide4,
            "hide5": hide5, "hide6": hide6, "hide7": hide7, "hide8": hide8, "hide9": hide9,
        }
        return render(request, 'projects_detail.html', context)

1 个答案:

答案 0 :(得分:1)

sh sub.sh

subScriptExitCode=$?

log "subScript ExitCode: $subScriptExitCode"

if [ $subScriptExitCode -ne 0 ]; then
    exit $subScriptExitCode
fi

您最好使用字典,希望我能帮助您