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.

Posted in  | Tags , ,  | no comments | no trackbacks

Rails and fastcgi problem

Posted by Y Ddraig Wen Thu, 10 Jan 2008 22:29:00 GMT

Ever since I've moved my work Rails site into production I've had fastcgi problems.  Yes, I'm using a shared host.  Yes, I know this is always going to be a problem.  And yes, I've hopefully found a solution.  One solution was given by Chris Gaskett and solved problems where errors caused fastcgi to stop running but didn't restart a problem.  This is fine for already running fastcgi threads, but not so much when there aren't any running.  If there are no fastcgi threads running the host starts up ten, reaching the max number, and causes a 500 error.  So the key was to create a cron job that would regularly start up the server so that it isn't (or is rarely) at zero processes.  And thanks to A Single Programmer's Blog I think I have the answer.  Let's just hope it works.

Tags: , ,

Posted in  | no comments | no trackbacks

Torchwood, bwyd a gweithio

Posted by Y Ddraig Wen Thu, 10 Jan 2008 22:00:00 GMT

Dw i newydd glywed y bydd Torchwood yn dechrau dydd Mercher nesaf.  Dw i'n cyffroi iawn.  Dw i'n mwynhau gwylio Torchwood, yn enwedig gwylio am heolydd fod dw i'n cydnabod. Ces i Shepherds Pie am cynio heddiw.  Mae e'n blasus.  Ond, bwytais i gormod.  Fel arfer, gwnes i ennill pwysar ôl Nadolig.  Does dim rhaid i mi gormod o fwyd am ennyd. Dechrais i fy swydd newydd wythos diwethaf.  Dw i'n brysur iawn.  Ond, dw i'n mynhau fy swydd newydd.  Mae e'n cyfrifoldeb mawr.

Posted in  | no comments | no trackbacks

First Impressions: TurboGears and Django

Posted by Y Ddraig Wen Sat, 05 Jan 2008 15:51:00 GMT

I've done a polls tutorial on Django and a wiki tutorial on TurboGears.  I'm not ready to pick between the two yet.  Both had things I very much liked and I'm too new to Python to make a snap decision.  But here's a few things that I've noticed/liked/disliked so far.

  • Both have an inbuilt admin area - Django's uses a password and is accessible in a subdirectory called admin.  You specify what the admin interface should and shouldn't do with relation to your models and can customise it fairly substantially in a short amount of code.  TurboGears' uses a different port rather than a password, it does a lot more in regards to the TurboGears setup but doesn't seem customisable with regards to models.
  • Both have one controller file and one model file for the entire application with separate view files.  This makes sense with Django which can have lots of apps plugged into an overall project, but could scale badly with TurboGears - although I suppose some form of importing might help.
  • Django has a template language very much like Rails.  TurboGears' is less familiar but does have nice code to replace the contents of an html tag with something generated by Python code.  Both can use different templating languages if you wish.
  • TurboGears uses MochiKit which is a javascript library for doing ajaxy stuff.  Django doesn't seem to handle javascript out of the box but that could be the choice of tutorials.
  • Both come with a web server for development purposes.  The one in TurboGears kept stopping or crashing with bad code.
  • To create or change SQL tables you write specific code in the model file and then run a script which then updates your database.  Not quite the flexibility of migrations but pretty cool and at least it doesn't involve yet another file.  Your attribute information is there in the model.  With TurboGears sqlite ran straight out of the box, Django needed a little configuration.
  • Django has some great "find" methods which will even redirect to a 404 if there's no model found.  This has to do with exceptions in TurboGears.
  • Django has some kind of hidden scaffolding for some of the common methods.
  • TurboGears just returns a dictionary of variables which the template (@expose'd before the method) then uses.  Django specifies both the template and the variables in a render_to_response method.  Very similar but I feld the Django one a little more intuitive.
  • Django uses regular expressions for urls (a bit like Rail's routes file but with reg exp).  This is quite scary to someone who melts at the sight of a reg exp, however it's also very customisable.  I have no idea what TurboGears does.  I think it just looks for methods named after the page called.
I think I prefer Django so far.  I'm going to try two more tutorials and then I'm going to attempt to write an incredbly simple  bit of blog software in both so I can properly compare.  The one thing greatly in Django's favour at the moment is the discovery of Feedjack which does exactly I want for the front page of my website.

Posted in  | no comments | no trackbacks

Python Fun

Posted by Y Ddraig Wen Thu, 03 Jan 2008 23:02:00 GMT

Decided I should try to learn Python.  Decided I want to use it to do a website.  So playing with Django (and will be playing with TurboGears at a later date).  Currently following this tutorial.  Having fun.  Expect more Python updates in future.

Posted in  | Tags ,  | no comments | no trackbacks