Hello I’m a Django Beginner.
I have a calendar that I want to be displayed on every site of my webpage, so I call this {% include "tags/my_calendar.html" %} in my base.html.
As I know so far, I can either display the calendar by creating a custom template tag or by using it in a standard Django view. I tested it with a standard Django view, by calling the calendar function through a urls.py, that works fine.
But I don’t want the calendar being only displayed for a special url.
So can I do this by defining a custom template tag?
I tried this so far:
from django import template
register = template.Library()
@register.inclusion_tag("tags/my_calendar.html")
def calendar():
"""
Show calendar of events for specified month and year
"""
lToday = datetime.now()
lYear = lToday.year
lMonth = lToday.month
my_workouts = ContestEvent.objects.filter(id__year=lYear, id__month=lMonth)
lCalendar = WorkoutCalendar(my_workouts).formatmonth(lYear, lMonth)
return {'calendar': mark_safe(lCalendar)}
I override the HTMLCalender class as follows and all this works with Django standard view as mentioned before, but returning a render_to_response:
class WorkoutCalendar(HTMLCalendar):
def __init__(self, workouts):
super(WorkoutCalendar, self).__init__()
# self.workouts = self.group_by_day(workouts)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
return self.day_cell(cssclass, day)
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(WorkoutCalendar, self).formatmonth(year, month)
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
My \tags\my_calendar.html looks like this:
{% load calendar_tag %}
<div>
{% calendar %}
</div
What am I doing essentially wrong? I got the following error:
Request Method: GET
Request URL: http://127.0.0.1:8000/de/
Exception Type: TemplateSyntaxError
Exception Value:
Caught an exception while rendering: maximum recursion depth exceeded
update1
With the hint of Daniel Roseman to make use of variable syntax {{ calendar }} instead of the tag syntax {% calendar %} I got rid of the recursion error, but my calendar is still not displayed. I make a lot of investigations.
What is wrong here?
update2
after some investigation, I found this helpful short description on how to use custom template tag.
The mistake why the calender didn’t show up, was that I was calling {% include "tags/my_calendar.html" %} in the base.html, instead of calling the function of my tag as {% calendar <parameter> %}. Whereas my tag template was ok, which calls the returned value of the tag function:
{{ calendar }}
Your recursion error is because you are calling the
calendartag inside the template being rendered by that very same tag.Your /tags/my_calendar.html file should actually look like this:
Note the use of the variable syntax, to show you’re rendering the value passed into the context, rather than the tag syntax. You don’t need to use
load, either.