Django blog so far
Posted by Y Ddraig Wen Thu, 10 Jan 2008 23:12:00 GMT
So far, other than a little url configuration, I've barely written any Python. I've done more with django's template code. But I've got a way to display various blog pages - latest, by year, by month, by day and entry page. The entry page shows comment count and has a comment form - although there is now way to deal with comment submission yet. Entries are quite simply added via the inbuilt admin interface which makes life much easier. I've spent perhaps a few hours on it so far - and only that long because of problems with regular expressions and because I'm still learning about Python, Django, and the template language. But it means I can quite easily work for an hour a night and get quite a lot done. Here's some code if you are interested!
models.py
from django.db import models
from django.contrib.auth.models import User
from time import strftime
class Category(models.Model):
name = models.CharField(max_length=30)
class Admin:
pass def
__str__(self):
return self.name
class Entry(models.Model):
title = models.CharField(max_length=50)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
short_text = models.TextField()
full_text = models.TextField()
category = models.ForeignKey(Category)
author = models.ForeignKey(User)
def __str__(self):
return '%s (%s)' % (self.title, self.date_created.strftime('%d-%m-%Y'))
class Admin:
date_hierarchy = "date_created"
fields = ( (None, { 'fields': ('title', 'short_text', 'full_text', 'category', 'author') }), )
list_filter = ('category', 'date_created', 'date_updated', 'author')
list_display = ('__str__',)
urls.py
from django.conf.urls.defaults import *
from draigwen.blog.models import Entry, Category
info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'date_created',
'extra_context': {
'category_list' : Category.objects.all(),
},
'allow_empty': True
}
year_dict = info_dict.copy()
year_dict['make_object_list'] = True
entry_dict = {
'queryset': Entry.objects.all(),
'date_field': 'date_created',
'extra_context': {
'category_list' : Category.objects.all(),
}
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<object_id>\d)/$', 'object_detail', entry_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', info_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', info_dict),
(r'^(?P<year>\d{4})/$', 'archive_year', year_dict),
(r'^$', 'archive_index', info_dict), )
urlpatterns += patterns('draigwen.blog.views',
(r'^category/(?P<category>\w+)/$', 'category_page'), )
And that is literally all the code. I'm not really happy with the three fairly repetitive dictionaries but I haven't worked out the best way around it. I'll post the template code when I'm a bit happier with it. Next up - get comments working properly, tidy up views so they show what I want how I want it, and do category pages.
