As you can read, I’ve been traveling around quite a bit lately. This means I haven’t been checking the comments on my blog, which means quite a bit of spam has been entered. I am blocking the spam via akismet, however, it is still recorded in the database. Being somebody who hates cluttered desktops, you can imagine how I feel about having a lot (447) of spam. Well, since akismet flips the is_public switch True for good comments and False for bad comments, that makes a really easy query in mysql.
Read Mass Spam Delete DjangoThis is fairly well documented in the Django docs, so I’ll be brief. This is the the bit of search code I use in almost all of my Django sites, and it works great:
def search(request): from django.db.models import Q q = request.GET.get("q", "") if q and len(q) >= 3: clause = Q(dirtword__icontains=q) \ | Q(description__icontains=q) \ | Q(tags__name__icontains=q) site_search = Dirt.objects.filter(clause).distinct() else: site_search = Dirt.objects.order_by('?')[:100] return list_detail.object_list( request = request, queryset = site_search, template_name = "dirty/search.html", template_object_name = "dirty", paginate_by = 20, extra_context = {"q" : q}, ) …
Read Adding Search to DjangoCreating feeds in Django is freaking simple. I’ll start with an example of just updating a feed with the newest objects (for instace, newest blog posts). Similar to the forms.py way of handling our different forms, I’ve created a feeds.py to handle the feeds.
from django.contrib.syndication.feeds import Feed from colddirt.dirty.models import Dirt class LatestDirts(Feed): title = "Cold Dirt" link = "/" description = "When you have dirt, you've got dirt. Right..." copyright = 'All Rights Unreserved' def items(self): return Dirt.objects.all()[:50] What this will do is query our Dirt DB and return an obj. The fields here are pretty well documented in the Django docs, …
Read Django Syndication with ColddirtSince I’ve already covered a really simple syndication example, I’ll move onto something a little more complex. Let’s say you want to offer syndication that is slightly more custom. The Django syndication docs give an example from Adrian’s Chicagocrime.org syndication of beats. I had to ponder a minute to get “custom” syndication to work, so here’s my example from start to finish.
First, as usual, feeds.py
class PerDirt(Feed): link = "/" copyright = 'Copyright (c) 2007, Blog Mozaic' def get_object(self, bits): from django.shortcuts import get_object_or_404 if len(bits) != 1: raise ObjectDoesNotExist my_dirt = get_object_or_404(Dirt, slug__exact=bits[0]) …
Read Django Syndication with Colddirt III hear many complaints and questions about newforms, but I’ve personally found it rather easy and logical to use. There are numerous ways for you to use do forms in Django, and most likely the best way to see them all is to read the docs. On the Colddirt demo site, this is how I used newforms. I’ll take the index page as an example.
I’ve accessed the newforms module like so:
Read Django Newforms Usage in ColddirtSo, the Django developers, in my opinion, are freaking smart. Instead of bundling Django with a particular library, they have added XML and JSON serialization; us humble users can choose whatever AJAX library we want. Prototype 1.5.1 has been pretty fun to work with, so I’ll kick off this demo with a really simple example.
How simple? The intended goal is to have the total number of ‘dirts’ update without user intervention. Laaaammmeee. If you are a visual type of person, take a look on the Colddirt huh page. That number automatically increases without user intervention. And this is how.
The process (some pseudocode) will go like this:
Read Simple Ajax with DjangoNote: Colddirt’s source code is run from Django SVN, check out on May 10th-ish. If you are using a newer branch, some things have changed. i.e. clean_data has been renamed cleaned_data. Remember to check the BackwardsIncompatible page in the wiki.
Part 1: Simple AJAX with Django
Part 2: Django Newforms Usage
Part 3: Search with Django
Part 4: Django’s Syndication Framework (simple)
Part 5: Django’s Syndication Framework (little more complex)
Feel free to see the source code from Github.
Read Colddirt InformationI’m writing this just in case somebody runs into this same issue. I’m about to go live with a website and figured it would be best to have the latest SVN snapshot checked out from Django. I updated, and noticed that my voting module didn’t quite work as expected. I was getting the following error:
'module' object has no attribute 'GenericForeignKey' I jumped into Trac and noticed that just yesterday some things were rearranged. In short, if you are using generic relations, you’ll need to change two parts of your code. First, the generic relations field must be imported out of conttenttype.
Read Django SVN Update Goes SplatWell, I’m basically all done upgrading to Version 3.0, I deserve a cake or something. Here’s the 411:
For the past few years I have been using Mambo, then Joomla, to manage the content on my site. It worked quite well, and was in PHP, so I could add or remove any code. Indeed, I’ve written a decent amount of PHP apps. In early 2004 I wrote a PHP platform to track adventures people had gone on, and networked people seeking to go on adventures with each other. I never marketed it, and mainly created it to learn PHP, but it was a CMS (Content Management System), and a little more. Late in 2004 I wrote another blog-esque platform for my second trip to Europe. It was pretty cool, I’ll admit: …
Read Version 3.0 Part Two