1

Alguien puede ayudarme con este error en Django con python 2.7. Estoy tratando con este codigo implementar un calendario.
Estoy teniendo un error en la variable context['calendar'] = mark_safe(html_cal) Nose que estoy haciendo mal.

 from datetime import datetime
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.utils.safestring import mark_safe

from .models import *
from .utils import Calendar

class CalendarView(generic.ListView):
    model = Event
    template_name = 'agendas/templates/calendar.html'

def get_context_data(self, **kwargs):
    context = super(CalendarView,self)

    # use today's date for the calendar
    d = get_date(self.request.GET.get('day', None))

    # Instantiate our calendar class with today's year and date
    cal = Calendar(d.year, d.month)

    # Call the formatmonth method, which returns our calendar as a table
    html_cal = cal.formatmonth(withyear=True)
    context['calendar'] = mark_safe(html_cal)
    return context

def get_date(req_day):
    if req_day:
        year, month = (int(x) for x in req_day.split('-'))
        return date(year, month, day=1)
    return datetime.datetime.today()

El error en el log de Django

    'super' object does not support item assignment
    context['calendar'] = mark_safe(html_cal)

Aqui esta descrito la variable context.

<super: <class 'CalendarView'>, <CalendarView object>>
d   datetime.datetime(2020, 3, 25, 23, 26, 52, 368050)
self    <comutechapps.agendas.views.CalendarView object at 0x7fbb7d6d0250>
cal     <comutechapps.agendas.utils.Calendar object at 0x7fbb79e94a90>
html_cal    '<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n{self.formatmonthname(self.year, self.month, withyear=withyear)}\n{self.formatweekheader()}\n{self.formatweek(week, events)}\n{self.formatweek(week, events)}\n{self.formatweek(week, events)}\n{self.formatweek(week, events)}\n{self.formatweek(week, events)}\n{self.formatweek(week, events)}\n'
kwargs {}
0

1 respuesta 1

2

El error está en la línea:

context = super(CalendarView,self)

super devuelve un objeto proxy que delega llamadas de método a una clase padre siguiendo el MRO. No es la clase misma. En tu caso contex por tanto es simplemente un objeto tipo super, por lo que cuando posteriormente haces:

context['calendar'] = mark_safe(html_cal)

intentas asignar a la clave calendar y super no es ningún contenedor que defina el método __getitem__ como es dict, por lo que tienes el error mencionado.

El problema es que te falta la llamada al método de la clase padre:

context = super().get_context_data(**kwargs)

o si aún usas Python 2.x:

context = super(CalendarView, self).get_context_data(**kwargs)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.