问题
I am a Django Beginner. I found this Django snippet to show a simple calendar on my web page. The function needed 3 parameters that one can provide within the template as follows:
{% load calendar_tag %}
<div>
<div>Calendar: </div>
{% get_calendar for 10 2010 as calendar %}
<table>
<tr>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
{% for week in calendar %}
<tr>
{% for day in week %}
<td>{{ day.day }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
How can I provide the number of the month and the year dynamically from the server, for example like:
{% now "jS F Y H:i" %}
It is not possible to have a block tag within a block tag. I would appreciate to use this simple calendar. Thanks in advance.
回答1:
I would presume you would pass the params in when rendering the template in your view, like so:
def yourview(request, month, year):
return render_to_string("template.html", {
"month": month,
"year": year,
})
and then change get_calendar
accordingly:
{% get_calendar for month year as calendar %}
回答2:
I want to present, how I fixed my problem, for others who might have similar problems:
With the help of the user fish2000, I managed my question like this:
My defined view:
# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
import datetime
def calender_view(request, template_name="custom_template_calender_tag.html"):
d = datetime.date.today()
recent_month = d.month
recent_year = d.year
return render_to_response(template_name, locals(),context_instance=RequestContext(request))
and then changed get_calendar
from
{% get_calendar for 10 2010 as calendar %}
to
{% get_calendar for recent_month recent_year as calendar %}
to provide and render the recent month and year dynamically in my template custom_template_calender_tag.html
.
来源:https://stackoverflow.com/questions/4043509/django-provide-month-and-year-number-as-parameter-in-a-template-for-use-in-temp