Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Django 1.0

Introduction to Django 1.0

his presentation was delivered at the PyConPL 2008 conference.

Marcin Mierzejewski

October 18, 2008
Tweet

More Decks by Marcin Mierzejewski

Other Decks in Programming

Transcript

  1. History 2003 - Kansas, USA 2005 - BSD 2005 -

    v0.90 2006 - v0.95 2007 - v0.96 2008 - Fundation, v1.0 7
  2. Model class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(unique=True) class

    Entry(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique=True) body = models.TextField() data = models.DateTimeField(default=datetime.now) categories = models.ManyToManyField(Category)
  3. Model API >>> category = Category(slug='django', name='Django') >>> category.save() >>>

    print category.name u'Django' >>> categories = Category.objects.all() >>> categories = Category.objects.filter(slug='django') >>> categories [<Category: Category object>] >>> entry = Entry(slug='welcome', title='Welcome', body='') >>> entry.save() >>> entry.categories.add( category[0] ) >>> print entry.categories.all() [<Category: Category object>]
  4. View def entry_list(request): entries = Ebtry.objects.all()[:5] return render_to_response('list.html', {'entries': entries})

    def entry_details(request, slug): entry = get_object_or_404(Entry, slug = slug) return render_to_response('details.html', {'entry': entry})
  5. Template {{ variables }}, {% tags %} & filters <html>

    <head> <title>My Blog</title> </head> <body> {% for entry in entries %} <h1>{{ entry.title|upper }}</h1> {{ entry.body }}<br/> Published {{ entry.data|date:"d F Y" }}, <a href=”{{ entry.get_absolute_url }}”>link</a>. {% endfor %} </body> </html>
  6. Template Inheritance <html> <head> <title> {% block title %} {%

    endblock %} </title> </head> <body> {% block body %} {% endblock %} </body> </html> {% extend “body.html” %} {% block title %} Main page {% endblock %} {% block body %} Content {% endblock %}
  7. Webapp # django-admin.py startproject webapp webapp/ __init__.py manage.py settings.py urls.py

    webapp/ __init__.py manage.py settings.py urls.py DATABASE_ENGINE DATABASE_NAME INSTALLED_APPS = ( ... 'django.contrib.admin',) (r'^admin/(.*)', admin.site.root)
  8. Tabels webapp# manage.py syncdb Creating table auth_permission Creating table auth_group

    Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log ......................... webapp# manage.py runserver Validating models... 0 errors found Django version 1.0-final-SVN-unknown, .......... Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
  9. models.py class Kategoria(models.Model): nazwa = models.CharField(max_length=200) slug = models.SlugField(unique=True) def

    __unicode__(self): return u'%s' % self.nazwa class Wpis(models.Model): tytul = models.CharField(max_length=200) slug = models.SlugField(unique=True) tresc = models.TextField() data = models.DateTimeField(default=datetime.now) kategorie = models.ManyToManyField(Kategoria) def __unicode__(self): return u'%s' % self.tytul webapp/blog/models.py class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(unique=True) admin.site.register(Category) class Entry(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique=True) body = models.TextField() data = models.DateTimeField(default=datetime.now) categories = models.ManyToManyField(Category) admin.site.register(Entry)
  10. views.py def entry_list(request): entries = Entry.objects.all().order_by('-data')[:5] return render_to_response('list.html', {'entries': entries})

    def entry_details(request, slug): entry = get_object_or_404(Entry, slug = slug) return render_to_response('details.html', {'entries':entries})
  11. Templates <html> <head><title>My Blog</title></head> <body> {% for entry in entries

    %} <h1>{{ entry.title|upper }}</h1> {{ wpis.body }}<br> Published {{ entry.data|date:"d F Y" }}<br/> <a href="/{{ entry.slug }}/">Link</a> {% endfor %} </body> </html> <html> <head><title>{{ entry.title|upper }}</title></head> <body> <h1>{{ entry.title|upper }}</h1> {{ entry.body }}<br> Published {{ entry.data|date:"d F Y" }}<br/> </body> </html>
  12. Statistics views.py + models.py + settings.py + urls.py = 26

    LoC list.html + details.html = 19 LoC