introduction to Django in five slides

1,555 views

Published on

a brief introduction to Django in five slides. presented during a pre-conference session at code4lib 2010 in Asheville, NC. presenting again at code4lib 2011 NDNP pre-pre-conference in Bloomington, IN.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,555
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

introduction to Django in five slides

  1. 1. Djangoin a whirlwind
  2. 2. URL routing# Entriesurl(r^entry/(?P<entry_id>[0-9]+)/$, entry, name=entry),url(r^entry/new, entry_new, name=entry_new),url(r^entry/(?P<entry_id>[0-9]+)/edit/$, entry_edit, name=entry_edit),url(r^entry/(?P<entry_id>[0-9]+)/delete/$, entry_delete, name=entry_delete),# Tags (site-wide)url(r^tags/$, tags, name=tags),url(r^tag/(?P<tag_name>[+w:._-]+)/$, tag, name=tag),url(r^tag/(?P<tag_name>[+w:._-]+)/feed/$, tag_feed, name=tag_feed),
  3. 3. modelsclass EntryTag (m.Model): entry = m.ForeignKey(Entry, related_name=tags, db_index=True) tag = m.ForeignKey(Tag, related_name=entries, db_index=True) sequence_num = m.SmallIntegerField(default=0, db_index=True) class Meta: unique_together = [entry, tag, sequence_num] ordering = [sequence_num]
  4. 4. viewsdef tags (request): context = RequestContext(request) qs = m.EntryTag.objects.filter(entry__user__userprofile__is_private=False) qs = qs.exclude(entry__is_private=True) if request.user.is_authenticated(): qs = apply_user_filters_to_entry_tags(request, qs) qs = qs.values(tag__name).annotate(Count(tag)) qs_count = qs.order_by(-tag__count) count_paginator, count_page = pagify(request, qs_count) qs_alpha = qs.order_by(tag__name) alpha_paginator, alpha_page = pagify(request, qs_alpha) return render_to_response(tags.html, { paginator: count_paginator, page: count_page, alpha_paginator: alpha_paginator, alpha_page:alpha_page, }, context)
  5. 5. templates<td width=50% valign=top> <h3>Sorted by count</h3> <table id=tags class=gentable cellspacing=1> <tbody> {% for pair in page.object_list %} <tr> <th valign=top>{{ pair.tag__count }}</th> {% if browse_user %} <td class=coll1 valign=top><a rel=tag href={%url user_tag browse_user.username,pair.tag__name %}>{{ pair.tag__name }}</a></td> {% else %} <td class=coll1 valign=top> <a rel=tag href={% url tag pair.tag__name %}> {{ pair.tag__name }}</a></td> {% endif %} </tr> {% endfor %} </tbody> </table></td>
  6. 6. advantages• easy to learn• healthy community• plays well with markup gurus• keeps getting better• free as in speech

×