Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Good afternoon. Tell me, please, what could be the matter – 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)’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in the line :

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

I tried to replace it with {{ item[column] }} – it didn’t help.

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading