Home > Software design >  Django views.py Exception Value: Could not parse the remainder:
Django views.py Exception Value: Could not parse the remainder:

Time:02-05

I get an Exception Value error:

Could not parse the remainder: '(column)' from 'item.get(column)'

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})

home.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>

error:

Exception Value:Could not parse the remainder: '(column)' from 'item.get(column)'

Error in the line :

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

I tried to replace it with {{ item\[column\] }} - it didn't help.

CodePudding user response:

You can not subscript or call methods in Django templates, hence {{ item.get(column) }} is not possible. This is often not a good idea anyway: you should pass the data in an accessible format to the template.

You thus prepare this as:

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})

then we can render this with:

<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>
  • Related