This document provides an overview of the Django web framework. It discusses Django's mission of encouraging rapid development and clean design. It demonstrates how to create a blog application with Django, including generating models, activating the admin interface, defining URLs and views, and using templates to display data. It also briefly compares Django to other frameworks like Ruby on Rails.
9. Overview of this Tutorial Brief introduction and philosophy Creating Django models The automatic admin interface Views (and URLs) Templates Comparison with other frameworks
10. Django's Mission Statement “Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.”
14. $ ./manage.py runserver Validating models... 0 errors found. Django version 0.96-pre, using settings 'myproject.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
18. Creating Models from django.db import models class Blog(models.Model): title = models.CharField(maxlength=200) class Post(models.Model): title = models.CharField(maxlength=200) body = models.TextField() blog = models.ForeignKey(Blog) pub_date = models.DateTimeField()
20. Activating the Admin Interface from django.db import models class Blog(models.Model): title = models.CharField(maxlength=200) class Admin: list_display = ['title'] class Post(models.Model): title = models.CharField(maxlength=200) body = models.TextField() blog = models.ForeignKey(Blog) pub_date = models.DateTimeField() class Admin: list_display = ['title', 'pub_date']
21.
22.
23. Model API $ ./manage.py shell >>> from myproject.blog import Blog >>> b = Blog( ... title=”Jason's Fantastic Blog!!!”) >>> b.save()
26. URLconfs from django.conf.urls.defaults import * from myproject.blog.views import * urlpatterns = patterns('', (r'^admin/', include('django.contrib.admin.urls')), (r'^blog/$', post_list), (r'^blog/(?P<id>+)/$', post_list), )
27. Views from django.http import HttpResponse def post_list(request): return HttpReponse(“This is a list of posts!”)
28. from django.http import HttpResponse from myproject.blog.models import Post def post_list(request): r = “<ul>” posts = Post.objects.order_by(“-pub_date”) for post in posts: r += “<li>%s: %s</li>” % (post.title, post.body) r += “</ul>” return HttpResponse(r) More realistic...
29. from django.shorcuts import render_to_response from myproject.blog.models import Post def post_list(request): posts = Post.objects.order_by(“-pub_date”) return render_to_response('blog/post_list.html', { 'post_list': posts, }) Better!
30. from django.shorcuts import render_to_response from myproject.blog.models import Post def post_detail(request, id): post = get_object_or_404(Post, id=id) return render_to_response('blog/post_detail.html', { 'post': post, }) For completeness...
31. Templates <html> <body> <h1>Jason's Fantastic Blog!!!</h1> <ul> {% for p in post_list %} <li> <a href=”{{ p.id }}/”>{{ p.title|escape }}</a> </li> {% endfor %} </ul> </body> </html>
Hello! My name is Jason Davies; I'm a freelance Web developer from Cambridge and I've been using Django for about 2 years ever since it was open-sourced in July 2005. Hopefully this will give you a good introduction to the basics of Django. Simon Willison will cover even more stuff in the advanced tutorial.