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 Technology | Tags blog, django, python | no comments | no trackbacks
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: rails, fastcgi, sharedhosting
Posted in Technology | no comments | no trackbacks
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 Technology | no comments | no trackbacks
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 Technology | Tags python, webdev | no comments | no trackbacks
Posted by Y Ddraig Wen
Sat, 22 Dec 2007 20:48:00 GMT
I was having trouble installing a perl module on Linux Mint despite the same module installing fine on the Linux Mint machine next to mine. Scrolling up through the errors it seemed that various .h files were missing. A quick search online and I discovered this post on the Ubuntu Forums. For those who have the same problem and don't want to read the post the solution is pretty damn simple:
sudo apt-get install build-essential
Or searchh for build-essential in synaptic and install it that way.
Posted in Technology | no comments | no trackbacks
Posted by Y Ddraig Wen
Sat, 08 Dec 2007 20:34:00 GMT
This is more a heads-up or reminder for myself. I discovered last week that for some reason the bilingual website I've been developing wasn't translating properly. Certain aspects of the Welsh side of the site were showing up in English despite already being translated. I thought at first that maybe something had been deleted in the database. Then I thought maybe it was a cache problem having recently moved from development mode to production mode. I couldn't seem to find any reference to the problem on the web. And then I realised that it was two specific models not translating: the two with tagging.
I was using the Globalize plugin for the bilingual aspects of the site and has_many_polymorphs for tagging (since it has a nifty little tagging generator). I removed has_many_polymorphs and all references to it and sure enough translations work fine again.
Strangely enough, things were working fine on my development computer despite using exactly the same versions of Rails and the two plugins. How very annoying.
Posted in Technology | Tags globalize, has_many_polymorphs, rails | no comments | no trackbacks
Posted by Y Ddraig Wen
Sat, 08 Dec 2007 20:10:00 GMT
The phrase above was coined by my other half but the idea was one we jointly came to through discussion. We're both used to LiveJournal and the idea of security settings and friends. While we both like the idea of having blogs hosted on our own domain we lose that aspect of LiveJournal. We can still watch other people's journals with RSS feeds and we can still comment on their journal with OpenID. But there doesn't seem to be anything out there that allows security settings like LiveJourna and even if there is our friends would need special logins for the site.
I was thinking about how you might get round this and I was thinking that OpenID would be ideal for authenticating some kind of blogging security/friends system. Or there could be a system like the Drupal CMS where you can create an account on one Drupal site and log into another. The problem with the Drupal idea is that it's dependent on everyone using the same blogging software. What would be better would be some kind of protocol - some addon to OpenID - that blogging platforms like LJ or Blogger, and blogging software like Typo and Mephisto, could use to allow a network of blogging friends.
I wish I was smart enough to come up with an idea how to do this, or connected enough to sell the idea to others. But for now it just sits here as a wish.
Posted in Technology | Tags blogging, security | no comments | no trackbacks