Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim

3,426
-1

Published on

Introduction to Django presentation given by me at OSSCamp Delhi held on September 08 and 09 2007

Published in: Technology, Design
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,426
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
201
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim

  1. 1. DJANGO <ul><li>WEB APPLICATION FRAMEWORK </li></ul><ul><li>FOR </li></ul><ul><li>PERFICTIONISTS WITH DEADLINES </li></ul>Presented by Mir Nazim www.xensoftlabs.com [email_address]
  2. 2. Django at Glance <ul><li>A high-level Web framework. </li></ul><ul><li>Eases the pain of building dynamic Web sites. </li></ul><ul><li>Abstracts common problems of Web development </li></ul><ul><li>Provides shortcuts for frequent programming tasks. </li></ul>
  3. 3. Django <ul><li>provides a method of mapping requested URLs to code that handles requests </li></ul><ul><li>makes it easy to display, validate and redisplay HTML forms </li></ul><ul><li>converts user-submitted input into data structures that can be manipulated conveniently </li></ul><ul><li>helps separate content from presentation via a template system </li></ul>
  4. 4. ...And Django <ul><li>conveniently integrates with storage layers </li></ul><ul><li>lets you work more productively, at a higher level of abstraction </li></ul><ul><li>simply gets out of your way </li></ul>
  5. 5. Django is a <ul><li>MVT architecture </li></ul><ul><ul><li>Models </li></ul></ul><ul><ul><ul><li>Describes you data structure/schema </li></ul></ul></ul><ul><ul><li>Views </li></ul></ul><ul><ul><ul><li>Controls what a user sees Controller in MVC snese </li></ul></ul></ul><ul><ul><li>Templates </li></ul></ul><ul><ul><ul><li>How user sees it </li></ul></ul></ul>
  6. 6. Sample App <ul><li>Polls Application </li></ul><ul><ul><li>There are polls </li></ul></ul><ul><ul><li>There are choices </li></ul></ul><ul><ul><li>User votes on polls </li></ul></ul>
  7. 7. Installation <ul><li>Install Python </li></ul><ul><li>Install Apache and mod_python </li></ul><ul><li>Get your database </li></ul><ul><ul><li>Django works with PostgreSQL, MySQL, Oracle and SQLite </li></ul></ul><ul><ul><li>We will use SQLite for our example </li></ul></ul><ul><ul><li>Install python bindings MySQLdb, psycopg, cx_Oracle or pysqlite </li></ul></ul><ul><li>Downlowload Django and Install it. </li></ul><ul><li>see </li></ul><ul><li>http://www.djangoproject.com/documentation/install/ </li></ul>
  8. 8. Start the project <ul><li>$ django-admin.py startproject mysite </li></ul><ul><li>$ cd mysite </li></ul><ul><li>$ ls </li></ul><ul><li>__init__.py manage.py settings.py urls.py </li></ul><ul><li>What only four file? Hah! its joke </li></ul>
  9. 9. Start the app <ul><li>$ python ./manage.py startapp polls </li></ul><ul><ul><ul><ul><li>polls/ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>__init__.py </li></ul></ul></ul></ul><ul><ul><ul><ul><li>models.py </li></ul></ul></ul></ul><ul><ul><ul><ul><li>views.py </li></ul></ul></ul></ul><ul><li>Apps are stand-alone packages that are plugable from one django install to other </li></ul><ul><li>Apps are plugable as long as you make them so </li></ul>
  10. 10. Models <ul><li>In mysite/polls/models.py </li></ul><ul><ul><li>from django.db import models </li></ul></ul><ul><ul><li>class Poll(models.Model): </li></ul></ul><ul><ul><li>question = models.CharField(maxlength=200) </li></ul></ul><ul><ul><li>pub_date = models.DateTimeField('date published') </li></ul></ul><ul><ul><li>class Choice(models.Model): </li></ul></ul><ul><ul><li>poll = models.ForeignKey(Poll) </li></ul></ul><ul><ul><li>choice = models.CharField(maxlength=200) </li></ul></ul><ul><ul><li>votes = models.IntegerField() </li></ul></ul>
  11. 11. A little config <ul><li>In mysite/settings.py </li></ul><ul><ul><ul><li>INSTALLED_APPS = ( </li></ul></ul></ul><ul><ul><ul><li>'django.contrib.auth', </li></ul></ul></ul><ul><ul><ul><li>'django.contrib.contenttypes', </li></ul></ul></ul><ul><ul><ul><li>'django.contrib.sessions', </li></ul></ul></ul><ul><ul><ul><li>'django.contrib.sites', </li></ul></ul></ul><ul><ul><ul><li>'mysite.polls', </li></ul></ul></ul><ul><ul><ul><li>) </li></ul></ul></ul>
  12. 12. DB config <ul><li>In mysite/settings.py </li></ul><ul><ul><li>DATABASE_ENGINE = 'sqlite3' </li></ul></ul><ul><ul><li>DATABASE_NAME = 'mysite.sqlite' </li></ul></ul><ul><ul><li>DATABASE_USER = '' </li></ul></ul><ul><ul><li>DATABASE_PASSWORD = '' </li></ul></ul><ul><ul><li>DATABASE_HOST = '' </li></ul></ul><ul><ul><li>DATABASE_PORT = '' </li></ul></ul>
  13. 13. Spit the SQL <ul><li>$ python manage.py sql polls </li></ul><ul><ul><ul><ul><li>BEGIN; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>CREATE TABLE &quot;polls_poll&quot; ( </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;id&quot; serial NOT NULL PRIMARY KEY, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;question&quot; varchar(200) NOT NULL, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;pub_date&quot; timestamp with time zone NOT NULL </li></ul></ul></ul></ul><ul><ul><ul><ul><li>); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>CREATE TABLE &quot;polls_choice&quot; ( </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;id&quot; serial NOT NULL PRIMARY KEY, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;poll_id&quot; integer NOT NULL REFERENCES &quot;polls_poll&quot; (&quot;id&quot;), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;choice&quot; varchar(200) NOT NULL, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&quot;votes&quot; integer NOT NULL </li></ul></ul></ul></ul><ul><ul><ul><ul><li>); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>COMMIT; </li></ul></ul></ul></ul>
  14. 14. Make my DB <ul><li>$ python manage.py syncdb </li></ul><ul><li>Generates SQL from model and populates your database with the shema </li></ul>
  15. 15. Using ORM & DB API <ul><li>$ python manage.py shell </li></ul><ul><li>This takes care of Putting mysite on sys.path </li></ul>
  16. 16. <ul><li># Import the model classes we just wrote. </li></ul><ul><li>>>> from mysite.polls.models import Poll, Choice </li></ul><ul><li># No polls are in the system yet. </li></ul><ul><li>>>> Poll.objects.all() </li></ul><ul><li>[] </li></ul>
  17. 17. <ul><li># Create a new Poll. </li></ul><ul><li>>>> from datetime import datetime </li></ul><ul><li>>>> p = Poll(question=&quot;What's up?&quot;, pub_date=datetime.now()) </li></ul><ul><li># Save the object into the database. You have to call save() explicitly. </li></ul><ul><li>>>> p.save() </li></ul>
  18. 18. <ul><li>>>> p.id </li></ul><ul><li>1 </li></ul>
  19. 19. <ul><li># Access database columns via Python attributes. </li></ul><ul><li>>>> p.question </li></ul><ul><li>&quot;What's up?&quot; </li></ul><ul><li>>>> p.pub_date </li></ul><ul><li>datetime.datetime(2005, 7, 15, 12, 00, 53) </li></ul>
  20. 20. <ul><li># Change values by changing the attributes, then calling save(). </li></ul><ul><li>>>> p.pub_date = datetime(2005, 4, 1, 0, 0) </li></ul><ul><li>>>> p.save() </li></ul><ul><li># objects.all() displays all the polls in the database. </li></ul><ul><li>>>> Poll.objects.all() </li></ul><ul><li>[<Poll: Poll object>] </li></ul>
  21. 21. Automatic Admin UI <ul><li>A dynamic admin interface </li></ul><ul><li>It’s not just scaffolding </li></ul><ul><ul><li>it’s the whole house </li></ul></ul><ul><li>Actually might be usefull for you </li></ul>
  22. 22. How to do it <ul><li>In mysite/settings.py </li></ul><ul><ul><li>add &quot;django.contrib.admin&quot; in INSTALLED_APPS list </li></ul></ul><ul><li>In mysite/urls.py </li></ul><ul><ul><li>uncomment the line below “Uncomment this for admin:” </li></ul></ul><ul><ul><li>(r'^admin/', include('django.contrib.admin.urls')), </li></ul></ul><ul><ul><ul><li>$ python ./manage.py syncdb </li></ul></ul></ul>
  23. 23. Make Polls part of Admin <ul><li>class Poll(models.Model): </li></ul><ul><li># ... </li></ul><ul><li>class Admin: </li></ul><ul><li>pass </li></ul>
  24. 24. Views <ul><li>Views are data that user sees </li></ul><ul><ul><ul><li>But not neccessarily how data looks </li></ul></ul></ul>
  25. 25. Design your URLs <ul><li>All urls go into urls.py </li></ul><ul><li>Defined using regular expression </li></ul><ul><li>(regular expression, Python callback function [, optional dictionary]) </li></ul>
  26. 26. In urls.py <ul><ul><li>from django.conf.urls.defaults import * </li></ul></ul><ul><ul><li>urlpatterns = patterns('', </li></ul></ul><ul><ul><li>(r'^polls/$', 'mysite.polls.views.index'), </li></ul></ul><ul><ul><li>(r'^polls/(?P<poll_id>d+)/$', 'mysite.polls.views.detail'), </li></ul></ul><ul><ul><li>(r'^polls/(?P<poll_id>d+)/results/$', 'mysite.polls.views.results'), </li></ul></ul><ul><ul><li>(r'^polls/(?P<poll_id>d+)/vote/$', 'mysite.polls.views.vote'), </li></ul></ul><ul><ul><li>) </li></ul></ul>
  27. 27. Write a view <ul><li>from django.http import HttpResponse </li></ul><ul><li># /polls </li></ul><ul><li>def index(request): </li></ul><ul><li>return HttpResponse(&quot;Hello, world. You're at the poll index.&quot;) </li></ul><ul><li># /polls/<poll_id> </li></ul><ul><li>def detail(request, poll_id): </li></ul><ul><li>return HttpResponse(&quot;You're looking at poll %s.&quot; % poll_id) </li></ul>
  28. 28. Actually doing useful stuff <ul><li>from django.template import Context, loader </li></ul><ul><li>from mysite.polls.models import Poll </li></ul><ul><li>from django.http import HttpResponse </li></ul><ul><li>def index(request): </li></ul><ul><li>latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] </li></ul><ul><li>t = loader.get_template('polls/index.html') </li></ul><ul><li>c = Context({ </li></ul><ul><li>'latest_poll_list': latest_poll_list, </li></ul><ul><li>}) </li></ul><ul><li>return HttpResponse(t.render(c)) </li></ul>
  29. 29. Templates <ul><li>$ mydir templates </li></ul><ul><li>and in settings.py add the path to ”templates” ”TEMPLATES_DIR” </li></ul><ul><ul><ul><li>TEMPLATE_DIRS = ( </li></ul></ul></ul><ul><ul><ul><li>&quot;/home/mir/mysite/templates&quot;, </li></ul></ul></ul><ul><ul><ul><li>) </li></ul></ul></ul>
  30. 30. templates/index.html <ul><li>{% if latest_poll_list %} </li></ul><ul><li><ul> </li></ul><ul><li>{% for poll in latest_poll_list %} </li></ul><ul><li><li>{{ poll.question }}</li> </li></ul><ul><li>{% endfor %} </li></ul><ul><li></ul> </li></ul><ul><li>{% else %} </li></ul><ul><li><p>No polls are available.</p> </li></ul><ul><li>{% endif %} </li></ul>
  31. 31. Decopuling the url config <ul><li>In settings.oy replace all polls urls with </li></ul><ul><ul><li>(r'^polls/', include('mysite.polls.urls')), </li></ul></ul><ul><li>Create a polls/urls.py </li></ul><ul><ul><ul><ul><li>urlpatterns = patterns('mysite.polls.views', </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(r'^$', 'index'), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(r'^(?P<poll_id>d+)/$', 'detail'), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(r'^(?P<poll_id>d+)/results/$', 'results'), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(r'^(?P<poll_id>d+)/vote/$', 'vote'), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>) </li></ul></ul></ul></ul>
  32. 32. There is more to it <ul><li>newforms package </li></ul><ul><ul><li>improves form handling than previous releases </li></ul></ul><ul><li>generic views </li></ul><ul><ul><li>no need to code obvious views </li></ul></ul><ul><li>Sessions </li></ul><ul><li>Caching, Internatalization, Localization, Custom Middleware, Authentication, RSS frameworks, Multi Site deploymens, </li></ul>
  33. 33. Resources <ul><li>www.djangoproject.com </li></ul><ul><li>www.djangosnippets.com </li></ul><ul><li>www.djangosite.org </li></ul>
  34. 34. Thank You <ul><li>? </li></ul>QUESTIONS

×