django - name 'context' is not defined -
i try use view create table (which later want populate data). if open respective url view creates "name 'context' not defined" error. can explain?
def room_overview(request, year, month): rooms = room.objects.all() long_month = ['01', '03', '05', '07', '08', '10', '12'] short_month = ['04','06','09','11'] if month in long_month: month_max = 31 elif month in short_month: month_max = 30 elif year % 4 == 0 , year %100 != 0 or year % 400 == 0: month_max = 29 else: month_max = 28 days = [] in range(1, month_max + 1): days.append(str(i)) context['rooms'] = rooms context['days'] = days context['month'] = month context['year'] = year return render(request, 'hotel/overview.html', context)
the template view looks this:
<h2>overview {{month}}/{{year}}:</h2> <div class="overview"> <table class="table table-condensed"> <tr> {% day in days %} <th>day</th> {% endfor %} </tr> {% room in rooms %} <tr> <td>{{ room.name }}</td> {% day in days %} <td> 1 </td> {% endfor %} </tr> {% endfor %} </table> </div>
this url entry:
url(r'^room/overview/(?p<year>[0-9]{4})/(?p<month>[0-9]{2})/$',views.room_overview, name='room_overview'),
you must define context variable first before add key/value pairs.
you can this:
context = {} context['rooms'] = rooms context['days'] = days context['month'] = month context['year'] = year
or, way prefer:
context = { 'rooms': rooms, 'days': days, 'month': month, 'year': year, }
and solve error.
Comments
Post a Comment