Reputation: 2641
Can someone explain me why am I getting back list index out of range
, it occur in two places in my view, first one is in context_data = self.get_context_data(user_id, month, year, form=form)
and context['month'] = calendar.month_name[month]
, so can someone explain me what is happening in my code. Here is a full Traceback
my view:
import calendar
import datetime
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.edit import FormView
from .helpers.user_util import get_user_full_name_or_user_name
from models import Rate
from statistics.forms import CommissionsMonthSelectionForm
from statistics.services import ManagerCommissionsProjectsCalculation, CommissionsAccessService
class UserSalesCommissionsView(FormView):
template_name = "statistics/user_sales_commissions.html"
form_class = CommissionsMonthSelectionForm
currency = Rate.EUR
def get(self, request, *args, **kwargs):
user_id = self.kwargs['user_id']
requesting_user = request.user
if CommissionsAccessService.can_view_commissions_page(requesting_user, user_id):
month = int(self.request.GET.get('month', datetime.datetime.now().month))
year = int(self.request.GET.get('year', datetime.datetime.now().year))
form = CommissionsMonthSelectionForm(initial={'year': year, 'month': month})
context_data = self.get_context_data(user_id, month, year, form=form)
return self.render_to_response(context_data)
else:
return HttpResponseForbidden('Access denied')
def post(self, request, *args, **kwargs):
user_id = self.kwargs['user_id']
requesting_user = self.request.user
if CommissionsAccessService.can_view_commissions_page(requesting_user, user_id):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form, user_id)
else:
return self.form_invalid(form)
else:
return HttpResponseForbidden('Access denied')
def form_valid(self, form, user_id=None):
year = form.cleaned_data['year']
month = form.cleaned_data['month']
return redirect(self.get_success_url(user_id, month, year))
def form_invalid(self, form):
messages.add_message(self.request, messages.ERROR,
"Invalid data. Please check fields.")
return self.render_to_response(
self.get_context_data(form=form)
)
def get_success_url(self,
user_id=None,
year=datetime.datetime.now().year,
month=datetime.datetime.now().month):
return reverse("vinclucms_statistics:manager_commissions_statistics", user_id) + \
"?month=" + str(month) + "&year=" + str(year)
def get_context_data(self,
user_id=None,
year=datetime.datetime.now().year,
month=datetime.datetime.now().month,
**kwargs):
context = super(UserSalesCommissionsView, self).get_context_data(**kwargs)
context['user_name'] = get_user_full_name_or_user_name(get_object_or_404(User, pk=user_id))
context['can_view'] = True
context['currency'] = self.currency
context['month'] = calendar.month_name[month]
context['year'] = year
context['commissions'] = ManagerCommissionsProjectsCalculation.get_project_list(
self.currency, year, month, user_id)
return context
views urls:
url(r'^statistics/commissions/sales/(?P<user_id>[^/]+?)/$', login_required(views.UserSalesCommissionsView.as_view()), name='user_sales_commissions')
Upvotes: 1
Views: 307
Reputation: 2688
month = datetime.datetime.now().month # IF Current month is April then month==4
calendar.month_name[month]
An array that represents the months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and month_name[0] is the empty string.
here : calendar.month_name[month]
check what is the value of month
it should be 4 because current month is April.
So you are passing String where it is required a datetime object.
Upvotes: 1
Reputation: 41116
Here's get_context_data
's definition:
def get_context_data(self,
user_id=None,
year=datetime.datetime.now().year,
month=datetime.datetime.now().month,
**kwargs):
And here's how it's called:
context_data = self.get_context_data(user_id, month, year, form=form)
so the 2 keyword arguments (year
and month
) are reversed yielding to something like calendar.month_name[2017]
which would spit the Indexerror
.
You should pass the arguments in the right order, or when dealing with keyword arguments, always specify their name:
context_data = self.get_context_data(user_id=user_id, month=month, year=year, form=form)
and that way their order would not be important any more.
Might also want to check PEP 3102.
Upvotes: 2