Django异常值:views.py Exception Value: Could not parse the remainder:

mm9b1k5b  于 2023-02-05  发布在  Go
关注(0)|答案(1)|浏览(159)

我收到异常值错误:
无法解析其余部分:"(列)"来自"项目.获取(列)"

网站views.py:

def home(request):
    position = DjangoEmail.objects.get(Email=request.user).Position
    year_filter = Q(Year=now.year) | Q(Year=now.year-1) | Q(Year=now.year+1)
    if position == 7:
        data = Employee.objects.filter(year_filter, Mlkk=request.user).order_by('Year','OblastTM').values('Year', 'OblastTM', 'Category', 'ProductGroup','NameChaine').annotate(Januaru=Sum('January'))
    elif position == 6:
        data = Employee.objects.filter(year_filter, Rmkk=request.user).order_by('Year','OblastTM').values('Year', 'OblastTM', 'Category', 'ProductGroup','NameChaine').annotate(Januaru=Sum('January'))
    elif position == 5:
        data = Employee.objects.filter(year_filter, Dmkk=request.user).order_by('Year','OblastTM').values('Year', 'OblastTM', 'Category', 'ProductGroup','NameChaine').annotate(Januaru=Sum('January'))    
    else:
        data = Employee.objects.filter(year_filter).order_by('Year','OblastTM').values('Year', 'OblastTM', 'Category', 'ProductGroup','NameChaine').annotate(Januaru=Sum('January'))
    columns = ['Year', 'OblastTM', 'Category', 'ProductGroupe', 'NameChaine','January']
    removed_columns = request.GET.getlist('remove')
    columns = [column for column in columns if column not in removed_columns]
  
    return render(request, "home.html", {'data': data, 'columns': columns})

主页. html:

<table>
        <thead>
            <tr>
                {% for column in columns %}
                <th>{{ column|title }}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for item in data %}
            <tr>
                {% for column in columns %}
                <td>{{ item.get(column)}}</td>
                {% endfor %}
            </tr>
            {% endfor %}
        </tbody>
</table>

错误:

异常值:无法分析其余部分:"(列)"来自"项目.获取(列)"
行错误:

<td>{{ item.get(column)}}</td>

我试着用{{ item\[column\] }}替换它-它没有帮助。

qkf9rpyu

qkf9rpyu1#

你不能在Django模板中使用下标或调用方法,因此{{ item.get(column) }}是不可能的。这通常不是一个好主意:应该将数据以可访问的格式传递给模板。
因此,您将其准备为:

from operator import itemgetter

def home(request):
    position = get_object_or_404(DjangoEmail, Email=request.user).Position
    year_filter = Q(Year__range=(now.year - 1, now.year + 1))
    columns = [
        'Year',
        'OblastTM',
        'Category',
        'ProductGroupe',
        'NameChaine',
        'Januaru',
    ]
    removed_columns = set(request.GET.getlist('remove'))
    columns = [column for column in columns if column not in removed_columns]
    queryset = Employee.objects.filter(year_filter)
    if position == 7:
        queryset = queryset.filter(Mlkk=request.user)
    elif position == 6:
        queryset = queryset.filter(request.user)
    elif position == 5:
        queryset = queryset.filter(Dmkk=request.user)
    queryset = (
        queryset.order_by('Year', 'OblastTM')
        .values('Year', 'OblastTM', 'Category', 'ProductGroupe', 'NameChaine')
        .annotate(Januaru=Sum('January'))
    )
    if columns:
        getter = itemgetter(*columns)
        if len(columns) == 1:
            data = [(getter(data),) for data in queryset]
        else:
            data = [getter(data) for data in queryset]
    else:
        data = ((),) * queryset.count()

    return render(request, 'home.html', {'data': data, 'columns': columns})

我们可以用以下公式来表示:

<thead>
    <tr>
        {% for column in columns %}
        <th>{{ column|title }}</th>
        {% endfor %}
    </tr>
</thead>
<tbody>
    {% for **row in data** %}
    <tr>
        {% for **cell in row** %}
        <td>{{ cell }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</tbody>

相关问题