Hello -

I'm playing around with a blog application and I want it to have a
tagging feature. I have the usual ManyToMany relationship set up with
Post and Tag (I'm not going to past my model since I think everyone is
familiar with this kind of setup).

I have three Posts tagged as follows:

    Post1: articles, python, django
    Post2: articles, python
    Post3: articles, music

Essentially, I would like to be able to go to a URL like:

    http://blog/tag/articles/python/django

And get Post1 as a result.

Likewise, I'd like to go to:

    http://blog/tag/articles/python

And get Post1 and Post3 as a result.

I have a URL entry set up as follows:

    (r'^blog/tag/(.*)', 'ssrc.blog.views.tags'),

I have my tags view set up like so:

def tags(request, url=""):
    if url:
        posts = {}
        for post in Post.objects.all():
            for tag in [part for part in url.split("/") if part != ""]:
                try:
                    post.tags.get(name__exact=tag)
                    posts[post.id] = post
                except:
                    posts[post.id] = None
        results = [post for post in posts.values() if post != None]
        if len(results) == 0:
            raise Http404
        else:
            return render_to_response("blog/tags.html", {
                'posts': results,
            })

This works. My question is just if there's a better way to accomplish
this. I realize that it'd be a lot cleaner if I just used hierarchical
categories. However, I'd like to keep this as flexible as possible by
using simple tags.

I apologize if this might have been covered elsewhere -- I did my best
to search the site and archives ahead of time, but I could have missed
something. 

Thanks!
-Joe


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to