Tag is probably the most distinguish feature of Web 2.0 applications that differentiate them from the traditional hierarchy categories. I want to attach a Web 2.0 tag to Gelman, so the user could simply click the tag, and find the related books that may arouse his/her interest.

django-taggingis a generic tag application to simplify the backend development, all you need to do is just add the TagField to the Book model:
class Book(models.Model):
……
tags = TagField()

And in the book_detail.html template, refer it as object.tags as this:

{{ object.tags|popuptags|safe }}

popuptags is a custom tag that decorates the tags as a list of HTML links, and join them:

@register.filter
def popuptags(value):
    tags = value.split(” “)
    return ” “.join([‘<a href="/bookshelf/tags/%s">%s</a>’ % (x, x) for x in tags])

So tag foo is linked with /bookshelf/tags/foo, so just redirect the request to the view:

(r‘^tags/(?P<tag_name>[\w-]+)/$’, views.by_tag)

And handle it in views.py:

@login_required
def by_tag(request, tag_name):
    tag = Tag.objects.get(name=tag_name)
    return list_detail.object_list(
        request,
        queryset=TaggedItem.objects.get_by_model(Book, tag),
        template_name=“bookshelf/book_list.html”,
        extra_context={‘title’: ‘Tagged by %s’ % tag_name},
    )

All the tedious work has been handled by django-tagging: we first get a tag object by name, and then build a QuerySet using get_by_model method; the rest is handled by the generic view, done. Salute to django-tagging developers!

We would discuss how to add a tag in the next post, that is the magic of Dojo.


14 Comments to “Learning Django by Example(7) Attach a tag”

  1. tonni | January 20th, 2008 at 4:43 pm

    no wonder u r a codemonkey :p

  2. David Grant | January 21st, 2008 at 7:06 pm

    Thanks for this, I’m going to go home and do this right away. I have a TagField on one of my model’s but I haven’t really done anything with it yet.

  3. Mr.Michaels | February 15th, 2008 at 2:29 pm

    Very cool siteRefactor the Life » Blog Archive » Learning Django by Example(7) Attach a tag was an eye catcher. I will not hassle you by posting comments here often but I am quite sure I will come back and read this as often as possible. Perhaps you might be interested in some of the software that I write, being a website owner and all. This post was automatically submitted to wordpress by dpfriend. I just wanted to make a quick ten dollar offer for the program I am using. Go to your favorite search engine and type dpfriend and you can find me there. I accept paypal. Also if you need anything else automated please let me know.

  4. Earth4Energy | August 28th, 2008 at 6:41 am

    Really tags are boon to web 2.0

  5. anja | September 27th, 2008 at 2:57 pm

    I say, 89% issue is dead, and there is a good replacement … See the forum Sierch.

  6. Imran | October 6th, 2008 at 3:21 am

    This tag is new to me….. Thanks for the post.

  7. free-stuff | October 6th, 2008 at 9:48 am

    Wow I just can’t seem to grasp it. I know I have to learn but it is just so darn confusing.

  8. Pariuri | October 13th, 2008 at 10:01 am

    I tried it but i don’t understand this part: “build a QuerySet using get_by_model method” If you can explain it more detailed it would be great.

    Thanks a lot mate!

  9. Website meta tags | October 18th, 2008 at 4:14 am

    Congratulations mate for this good written article, there is good info in it.

  10. Sam | October 18th, 2008 at 7:25 am

    Oh by god I thought getting the hang of basic HTML to embed stuff in my site was difficult!

  11. George | October 19th, 2008 at 10:28 am

    Thanks for the tutorial. Just the thing i was lookig for. Going to apply it now. Will let u know if i face probs :D

  12. Free Psychic and Tarot Readings stories | November 19th, 2008 at 6:06 pm

    I disagree with some of the given points, but nice post overall.

  13. KooWii | March 1st, 2009 at 3:18 am

    This one can help us the webmasters to create tags easily. No need to think hard for the suitable tags for our posts. The world is easier with this.

  14. check cashing software | March 26th, 2009 at 3:36 pm

    Great information, thanks!

Leave a Comment