首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何简化在django的views.py中呈现时使用for语句的代码

如何简化在django的views.py中呈现时使用for语句的代码
EN

Stack Overflow用户
提问于 2022-02-16 07:24:17
回答 1查看 39关注 0票数 0

我正在使用django,下面的代码工作效率很低。是否有一种方法可以通过使用如下代码中的for语句来缩短创建和附加列表的方法?列表用于在顶点图javascript中创建图形。

views.py

代码语言:javascript
复制
annotations = {}
types = ('A', 'B', 'C', 'D', 'E', 'F')
for type in types:
    annotations[type] = Count('id', filter=Q(type=type))
    annotations[f'r_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Recruiting'))
    annotations[f'N_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Not yet recruiting'))
    annotations[f'H_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Holding'))
    annotations[f'C_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Completed'))
    counts = Research.objects.values('teacher').annotate(**annotations).values('teacher', *annotations.keys())

teacher = [];
A = [];
B= [];
C= [];
D= [];
E= [];
F= [];
r_A = [];
r_B = [];
r_C = [];
r_D = [];
r_E = [];
r_F = [];
...
C_E = [];
C_F = [];
for count in counts:
    teacher.append(str(count['teacher']))
    A.append(str(count['A']))
    B.append(str(count['B']))
    C.append(str(count['C']))
    D.append(str(count['D']))
    E.append(str(count['E']))
    F.append(str(count['F']))
    r_A.append(str(count['r_A']))
    r_B.append(str(count['r_B']))
    r_C.append(str(count['r_C']))
    r_D.append(str(count['r_D']))
    r_E.append(str(count['r_E']))
    r_F.append(str(count['r_F']))
    ...
    C_E.append(str(count['C_E']))
    C_F.append(str(count['C_F']))

return render(request, 'graph.html',
              {
                  'teacher': teacher,
                  'A': A,
                  'B': B,
                  'C': C,
                  'D': D,
                  'E': E,
                  'F': F, 
                  'r_A': r_A,
                  'r_B': r_B,
                  'r_C': r_C,
                  'r_D': r_D,
                  'r_E': r_E,
                  'r_F': r_F,
                  ...
                  'C_E': C_E,
                  'C_F': C_F     
              })

graph.html

代码语言:javascript
复制
series: [{% if is_recruiting == 'Recruiting' %}
    {
      name: 'A',
      data: {{ r_A | safe }}
    }, {
      name: 'B',
      data: {{ r_B | safe }}
    }, {
      name: 'C',
      data: {{ r_C | safe }}
    }, {
      name: 'D',
      data: {{ r_D | safe }}
    }, {
      name: 'E',
      data: {{ r_E | safe }}
    }, {
      name: 'F',
      data: {{ r_F | safe }}
    },{% elif is_recruiting == 'ALL' %}
    {
      name: 'A',
      data: {{ A | safe }}
    }, {
      name: 'B',
      data: {{ B | safe }}
    }, {
      name: 'C',
      data: {{ C | safe }}
    }, {
      name: 'D',
      data: {{ D | safe }}
    }, {
      name: 'E',
      data: {{ E | safe }}
    }, {
      name: 'F',
      data: {{ F | safe }}
    }, ... {% elif is_recruiting == 'Completed' %}
    {
      name: 'A',
      data: {{ C_A | safe }}
    }, {
      name: 'B',
      data: {{ C_B | safe }}
    }, {
      name: 'C',
      data: {{ C_C | safe }}
    }, {
      name: 'D',
      data: {{ C_D | safe }}
    }, {
      name: 'E',
      data: {{ C_E | safe }}
    }, {
      name: 'F',
      data: {{ C_F | safe }}
    },{% endif %}
    ],

这是一个使用apexchart.js创建图形的javascript。此部分还使用if语句继续代码,因此可读性不好。有没有办法把这部分也缩短?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-16 07:59:46

类似于:

代码语言:javascript
复制
for count in counts:
    teacher.append(str(count['teacher']))
    A.append(str(count['A']))
    B.append(str(count['B']))
    C.append(str(count['C']))
    D.append(str(count['D']))
    E.append(str(count['E']))
    F.append(str(count['F']))

可以使用dict而不是多个列表重写:

代码语言:javascript
复制
count_dict = defaultdict(list)
for type_, count in itertools.product(types, counts):
    count_dict[type_].append(str(count[type_]))

(原因是type_而不是type,因为type是一个保留词.选择另一个变量名称,如。t (如果您不喜欢下划线)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71137756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档