Learning Django by Example(7) Attach a tag
Python, Web January 20th, 2008
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:
popuptags is a custom tag that decorates the tags as a list of HTML links, and join them:
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:
And handle it in views.py:
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.







no wonder u r a codemonkey :p
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.
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.
Really tags are boon to web 2.0
I say, 89% issue is dead, and there is a good replacement … See the forum Sierch.
This tag is new to me….. Thanks for the post.
Wow I just can’t seem to grasp it. I know I have to learn but it is just so darn confusing.
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!
Congratulations mate for this good written article, there is good info in it.
Oh by god I thought getting the hang of basic HTML to embed stuff in my site was difficult!
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
I disagree with some of the given points, but nice post overall.
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.
Great information, thanks!
What programming language is this? It reminds me of PHP but it’s obviously not…
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.
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.
Hello. On witch file do I have to put the popuptags function?