Sunday, October 26, 2008

django-tagging

I wanted to add some basic tagging to my blog app so I tried out django-tagging. Unfortunately, the featured downloads on the Google Code site are quite out-of-date and would not work with Django 1.0, so I did a subversion checkout instead. If you're getting an error like "ImportError: cannot import name parse_lookup", then you need to get the source code from SVN.

Adding the tagging to the blog was pretty easy:
1. Add the tagging app to settings.py
2. Add a tagging.fields.TagField to the Post model
3. Add a "tags" text field to the post form used.
4. Modify templates to display the tags.
5. I used something like "/tag/" url mapping to get all posts associated with a tag. Then you just need to write a wrapper around the object_list generic view:

from tagging.models import Tag, TaggedItem
from django.views.generic.list_detail import object_list

def posts_by_tag(request, tag):
o_tag = Tag.objects.get(name=tag)
queryset = TaggedItem.objects.get_by_model(Post, o_tag)
return object_list(request, queryset)


This view will use the same template used to list out posts normally.

Friday, October 24, 2008

I left it for a while...

...and now it works!

http://www.randomencounters.bells-n-whistles.net/blog/

I think the apache service needed to restart mod_python or something...(I don't have SSH access so...)

Tuesday, October 21, 2008

Deployment Problems

So I got a basic blog app up and running.

Posting, paged archives, etc.

Comments implemented using the django.contrib.comments. No problems here, the only caveat being most of the current documentation found by Google searches refer to the pre-1.0 version. Need to peruse the official docs for 1.0 stuff.

RSS feeds implemented using django.contrib.syndication, this one seems fine.

I tested it and it's running fine on localhost. I also have a free django hosting account at http://bells-n-whistles.net, so I try to upload it there. However, when I access the website there (http://www.randomencounters.bells-n-whistles.net/blog/), I get the following error:

'comments' is not a valid tag library: Could not load template library from django.templatetags.comments, No module named comments

I think the settings.py is not being reloaded - I tried to set Debug = False and I'm still getting the stacktraces.

Bah, I'll figure it out tomorrow.

Monday, October 20, 2008

"when redirecting, how can I make the redirect URL decoupled from the urls.py of the parent app?"

-> It turns out that HttpResponseRedirect supports relative paths, so this was fine.

return HttpResponseRedirect("../" + str(post.id) + "/")

I got the basic posting structure up.

/post/new/ -> To make new posts
/post// -> To view a single post
/post/all/ -> To view all posts

I should probably start thinking of a better url scheme. Ideally, I'd want the @login_required views to be indicated as such in the urls. Something like "/admin/post/" for new posts "/admin/manage/" for a screen to manage posts to differentiate it from a screen just to list them out.

Next I think I'll try to CSS-ify the blog; I'll probably just reuse stuff from one of the blogger templates as I'm still not very good with the HTML/CSS.

I'll also look into using some of the date-based generic views to get an archive view. I'll need more data though. Will consider importing from the Wordpress blog.

Sunday, October 19, 2008

Starting out

The quintessential app to learn from is of course a blog.

Started using a simple Post model. Added the new post form and view. Can now successfully insert posts into the DB.

Next:
- create the detail page that will show the post after saving

Figure out:
- when redirecting, how can I make the redirect URL decoupled from the urls.py of the parent app?

i.e. if the parent app has the following mapping:
'^blog/' -> pass to blog.urls.urlpatterns

the blog app has mappings for
'^post/new/' -> new post
'^post/([A-Za-z\-])/' -> post detail

inside the view, I want to redirect to "post/", but with respect to the app, it should be "blog/post/"

I should subscribe to one of the Django mailing lists to ask stuff like this.