Changes between Initial Version and Version 1 of CalendarRunThough


Ignore:
Timestamp:
Jul 8, 2007, 1:11:41 PM (17 years ago)
Author:
Carl Karsten <carl@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CalendarRunThough

    v1 v1  
     1Here is what happens when you browse to http://www.example.com/eventcal/2007/08/
     2
     3The URL is split into host/path, path is used to find a view:
     4
     5In this case, it gets done in 2 steps. 
     6the "eventcal" part is used to route things into the 'calendar app' which lives in eventcal:
     7
     8{{{
     9line 9 of ./urls.py:
     10     (r'^eventcal/', include('eventcal.urls')),
     11
     12line 7 of ./eventcal/urls.py
     13    (r'^(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_index'),
     14}}}
     15
     16The parameters are extracted: year and month - (?P<year>\d{4}) = decimal(4)
     17And calls month_index( request, year, month )
     18
     19(it can also be called with no year/month, which defaults to the current date.)
     20{{{
     21#!python
     22def month_index(request, year = None, month = None):
     23    now = datetime.today()
     24    if not year:
     25      year = now.year
     26    if not month:
     27      month = now.month
     28    year = int(year)
     29    month = int(month)
     30
     31    # Make sure we have a sane date.
     32    # A munged URL could give us month 13
     33    try:
     34        cal_date = date(year, month, 1)
     35    except ValueError:
     36        raise Http404
     37
     38    # Week starts on Sunday
     39    c=calendar.Calendar(calendar.SUNDAY)
     40
     41    # get all the dates that will be displayed,
     42    # including the tail and head of previous/next months.
     43    # (a list of weeks, each week is a list of 7 dates.)
     44    dates=c.monthdatescalendar(year, month)
     45
     46    # array, ['Monday', 'Tuesday'.. 0 is Mon.
     47    ## daynames=calendar.day_name
     48    # make list, cuz I can't figure out how to use the array in the template.
     49    # Use the first week of the calandar, which is handy
     50    # because it makes sure the week starts on the weekday we want it to
     51    # (like Sunday)
     52    daynames= [ calendar.day_name[d.weekday()] for d in dates[0] ]
     53
     54    # get Events from the DB that are in the specified month.
     55    # [0][0] = first day of first week, [-1][-1], last day of last week.
     56    # so start/end of the range being displaied.
     57    events = Event.objects.filter(eventdate__range=(dates[0][0],dates[-1][-1]))
     58
     59    # some fluf to make the page usable
     60    prev_month=(cal_date+relativedelta(months=-1))# .month
     61    next_month=(cal_date+relativedelta(months=+1))# .month
     62
     63    return render_to_response('month_index.html', locals())
     64}}}
     65
     66render_to_response() takes a template file  month_index.html and a bunch of vars (easy way: pass all the local vars) and returns browser ready HTML.  I should probably not use .html for these files, cuz they aren't;  but all the cool people use .html, so I'll fight that battle some other day.
     67
     68Template commands used here:
     69{# X #} one line comments.  {% comment %}X X X...{% endcomment %} multi line comment.
     70
     71{% extends X %} - kinda like subclass X.  kinda like #include if you don't pay attention too close.  but more like subclassing because  {% block Y %} defined in X can be overridden (replaced) if it is redefined in the current code.  in this case, main.html defines the 'stuff that will be on all pages' including some placeholders for headings and content,  and month_index.html defines the headings and content.  I can't figure out if it really is subclassing, or just similar.
     72
     73{{ X }} - expression evaluation - think VFPs <<X>> text merge.  X is evaluated, result is put in the HTML.  pipe char | is used like unix filters to format the results. /{{ prev_month|date:"Y/m" }} prev_month is a date. |date:"Y/m" makes a string out of the year/month.  so 6/1/2007 becomes "2007/6"  which makes a nice URL for handing into the top of this process. <a href="/eventcal/2007/6">prev</a>
     74
     75{% for week in dates %}
     76{% endfor %}
     77basic for loop. 
     78
     79{% ifequal day now.date %}
     80{% endifequal %}>
     81it's IF, but for some reason they made the operator? part of the command instead of "IF X" where X is a boolean.  no clue why.  possibly for speed.
     82
     83{{{
     84#!django
     85{# month_index.html #}
     86{% extends "main.html" %}
     87
     88{% block css %}
     89<link href="/static/css/calendar.css" type="text/css" rel="stylesheet" />
     90{% endblock %}
     91
     92{% block title %}Calendar{% endblock %}
     93
     94{% block list_title %}Events{% endblock %}
     95
     96{% block content %}
     97
     98{# Display month/year and links to previous/next month. #}
     99<div class="content-vol">
     100<h4 class="calendar_heading">
     101<a href="/eventcal/{{ prev_month|date:"Y/m" }}/"> &lt; </a>
     102{{ cal_date|date:"F" }} <a href="/eventcal/{{ cal_date.year }}/">{{ cal_date.year }}</a>
     103<a href="/eventcal/{{ next_month|date:"Y/m" }}/"> &gt; </a>
     104</h4>
     105
     106{# Make the grid of days #}
     107<table class="cal_month_calendar">
     108{# headers: Monday, Tuesday.... #}
     109<tr>
     110{% for dayname in daynames %}
     111<th>{{ dayname }}</th>
     112{% endfor %}
     113</tr>
     114
     115{# What we came here for: the days and events. #}
     116{% for week in dates %}
     117<tr>
     118{% for day in week %}
     119<td
     120{% ifequal day now.date %} class="today"
     121{% else %} {% ifequal day.month month %} class="cal_in_month"
     122  {% else %} class="cal_not_in_month"
     123 {% endifequal %}
     124{% endifequal %}>
     125
     126{{ day|date:"j" }}
     127
     128<p class="event_desc">
     129{% for event in events %}
     130  {% ifequal event.eventdate day %}
     131  <li> <a href="/eventcal/detail/{{ event.id }}/">{{ event.title }}</a></span> </li>
     132  {% endifequal %}
     133{% endfor %}
     134
     135</p>
     136
     137</td>
     138
     139{% endfor %}
     140</tr>
     141{% endfor %}
     142</table>
     143<br/>
     144
     145{# List the events in a list. #}
     146{% if events %}
     147<ul class="event_list">
     148{% for event in events %}
     149<li> <span class="cal_event_date">
     150  {% ifequal event.eventdate.month month %}
     151    {{ event.eventdate|date:"l j" }}
     152  {% else %}
     153    {{ event.eventdate|date:"F j" }}
     154  {% endifequal %}
     155  <a href="/eventcal/detail/{{ event.id }}/">{{ event.title }}</a></span>
     156  </li>
     157{% endfor %}
     158</ul>
     159{% else %}
     160<p>No activities this month.</p>
     161{% endif %}
     162
     163</div>
     164{% endblock %}
     165}}}
     166
     167Pretty much all main does is define the 'menu' that will be on all pages.
     168
     169{{{
     170#!django
     171{# main.html #}
     172{% extends "base.html" %}
     173{% block css %}{% endblock %}
     174{% block title %}Logged in{% endblock %}
     175{% block headtags %}
     176{% endblock %}
     177{% block content %}{% endblock %}
     178{% block foottags %}
     179<a href="/eventcal">Event Calendar</a>
     180<a href="/admin">Admin</a>
     181<a href="/messages">Messages</a>
     182<a href='/logout'>logout</a>
     183{% endblock %}
     184}}}
     185
     186base.html is the top most file on the food chain.
     187
     188{{{
     189#!django
     190{# base.html #}
     191<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     192<html xmlns="http://www.w3.org/1999/xhtml">
     193
     194<head>
     195  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     196  <title>My Great Site {% block title %}{% endblock %}</title>
     197  <link href="/static/css/style.css" rel="stylesheet" type="text/css" />
     198  {% block css %}{% endblock %}
     199</head>
     200
     201<body background="/static/images/background.png">
     202
     203  <div id="container">
     204 
     205   <div id="header">
     206        {% block headtags %}{% endblock %}
     207   </div>
     208
     209   <div id="main">
     210        {% block content %}{% endblock %}
     211   </div>
     212
     213   <div id="footer">
     214        {% block foottags %}{% endblock %}
     215   </div>
     216
     217
     218<table>
     219<tr>
     220<td valign="left"></td>
     221<td valign="top" class="subtle">
     222<hr size="1">
     223&#8226; phone number here
     224</td>
     225<td valign="left"></td>
     226</tr>
     227</table>
     228
     229  </div> <!-- /container -->
     230 </body>
     231</html>
     232}}}
Back to Top