Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created February 24, 2010 20:02
Show Gist options
  • Save jacobian/313787 to your computer and use it in GitHub Desktop.
Save jacobian/313787 to your computer and use it in GitHub Desktop.
#
# Further reading:
# Template builtins: http://docs.djangoproject.com/en/1.1/ref/templates/builtins/
# Generic view reference: http://docs.djangoproject.com/en/1.1/ref/generic-views/
#
#### entries/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('yabl.entries.views',
url(r'^$', 'index'),
url(r'^past/', 'past'),
url(r'^future/', 'future'),
url('^(\d{4})/$', 'year'), # 2009/
url('^(\d{4})/(\d{2})/$', 'month'), # 2009/04
url(r'^([\w-]+)/', 'detail', name="entry_detail"),
)
#### entries/views.py
import datetime
from django.shortcuts import render_to_response, get_object_or_404
from yabl.entries.models import Entry
from django.views.generic import date_based
def year(request, year):
return date_based.archive_year(request,
year = year,
queryset = Entry.objects.all(),
date_field = 'pub_date',
template_name = 'entries/year.html',
allow_empty = True,
)
def month(request, year, month):
return date_based.archive_month(request,
year = year,
month = month,
queryset = Entry.objects.all(),
date_field = 'pub_date',
template_name = 'entries/month.html',
allow_empty = True,
month_format = "%m",
)
def index(request):
return render_to_response('entries/index.html', {
'entries': Entry.objects.all(),
'title': 'Entry list',
})
def detail(request, slug):
return render_to_response('entries/detail.html', {
'entry': get_object_or_404(Entry, slug=slug),
})
def future(request):
return render_to_response('entries/index.html', {
'entries': Entry.objects.future(),
'title': 'Future entries',
})
def past(request):
return render_to_response('entries/index.html', {
'entries': Entry.objects.past(),
'title': 'Past entries',
})
##### templates/entries/year.html
{% extends "base.html" %}
{% block title %}{{ year }} archive | {{ block.super }}{% endblock %}
{% block content %}
{% for d in date_list %}
<li>{{ d|date:"F" }}</li>
{% endfor %}
{% endblock content %}
#### templates/entries/month.html
{% extends "base.html" %}
{% block title %}{{ month }} archive | {{ block.super }}{% endblock %}
{% block content %}
<ul>
{% for o in object_list %}
<li>{{ o }}</li>
{% endfor %}
</ul>
<a href="/entries/{{ previous_month.year }}/{{ previous_month|date:"m" }}/">prev</a>
{% if next_month %}
<a href="/entries/{{ next_month.year }}/{{ next_month|date:"m" }}/">next</a>
{% endif %}
{% endblock content %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment