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

Django tutorial

Django tutorial

Fossmeet @ NITC 2010

Avatar for Sarath Lakshman

Sarath Lakshman

February 17, 2010
Tweet

More Decks by Sarath Lakshman

Other Decks in Programming

Transcript

  1. Django Workshop Overview of this Tutorial ➔ Brief introduction and

    philosophy ➔ Views ➔ URL Conf ➔ Templates ➔ Admin interface ➔ Hands-on code ➔ Building a quick blog application
  2. Django Workshop Django's Mission Statement “Django is a high-level Python

    web framework that encourages rapid development and clean, pragmatic design.”
  3. Django Workshop Django Requirements ➔ Python (2.3+) ➔ PostgreSQL /

    MySQL / SQLite / ... ➔ Apache + mod_python / FastCGI / ...
  4. Django Workshop Installation $ sudo apt-get install python-django OR $

    wget http://www.djangoproject.com/download/1.1.1/tarball/ $ tar xzvf Django-1.1.1.tar.gz $ cd Django-1.1.1 $ sudo python setup.py install
  5. Django Workshop $ ./manage.py runserver $ ./manage.py runserver Validating models...

    0 errors found. Django version 1.1.1, using settings 'myproject.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C. Development server
  6. Django Workshop “Apps” $ django-admin.py startapp helloweb $ django-admin.py startapp

    helloweb INSTALLED_APPS INSTALLED_APPS 'myproject.helloweb' 'myproject.helloweb'
  7. Django Workshop URL Conf Collection URL maps which describes which

    url show map to which view function arranged in urls.py
  8. Django Workshop “Models” In Django, Models are the Data store.

    Models are high level object oriented interfaces for databases where the object schema is defined in model.py Forget about messes with “SELECT * FROM TABLE.. blah blah”
  9. Django Workshop Concept to Code 1. Create project – django-admin

    createproject 2. Settings - settings.py 3. Create application - ./manage.py startapp 4. Write Models scheme – models.py 5. Sync database - ./manage syncdb 6. Views & templates – view.py 7. URLConf – urls.py 8. Done
  10. Django Workshop Views from django.http import HttpResponse from django.http import

    HttpResponse def index(request): def index(request): html = “<h1>Hello Web !</h1>” html = “<h1>Hello Web !</h1>” return HttpResponse(html) return HttpResponse(html) views.py
  11. Django Workshop URL Conf from django.conf.urls.defaults import * from django.conf.urls.defaults

    import * urlpatterns = patterns('', urlpatterns = patterns('', (r'^helloweb/$', 'helloweb.views.index'), (r'^helloweb/$', 'helloweb.views.index'), ) ) urls.py
  12. Django Workshop More on Views # Processing GET # Processing

    GET request request def process_get(request): def process_get(request): html = “Not a GET request” html = “Not a GET request” if request.GET.has_key('page_no'): if request.GET.has_key('page_no'): html = “You have requested %d” % html = “You have requested %d” % request.GET['page_no'] request.GET['page_no'] return HttpResponse(html) return HttpResponse(html) Other dictionary objects Other dictionary objects request.POST request.POST request.session request.session
  13. Django Workshop Using Templates settings.py TEMPLATE_DIRS = ( "/home/slynux/helloweb/html_templates" )

    /home/slynux/helloweb/html_templates/template1.html <html> <head><title>HelloWeb django</title></head> <body> <p> This is a sample page</p> <table> <tr><td>Student</td><td>Roll No</td></tr> <tr><td>Sarath</td><td>22</td></tr> <tr><td>Slynux</td><td>44</td></tr> </table> </body> </html>
  14. Django Workshop Using Templates File: /home/slynux/helloweb/html_templates/template2.html <html> <head><title>HelloWeb django</title></head> <body>

    <p> This is a sample page</p> <table> <tr><td>Student</td><td>Roll No</td></tr> {% for name,rollno in student_list.items() %} <tr> <td>{{ name }}</td> <td>{{ rollno }}</td> </tr> {% endfor %} </table> </body> </html>
  15. Django Workshop View with Template def template_view1(request): from django.shortcuts import

    render_to_response return render_to_response('template1.html',{}) def template_view2(request): from django.shortcuts import render_to_response students = { 'Sarath':22, 'Slynux':34 } return render_to_response('template2.html', {'student_list':students } )
  16. Django Workshop Using Models / Datastore settings.py DATABASE_ENGINE = 'sqlite3'

    DATABASE_NAME = '/home/slynux/fossmeet.in/db.sqlite' models.py from django.db.models class StudentRegister(models.Model): name = models.CharField(max_length=30) rollno = models.IntegerField() admission_date = models.DateTimeField() $ ./manage.py syncdb Creating student_studentregister Loading 'initial_data' fixtures... No fixtures found. $./manage.py sql helloweb
  17. Django Workshop Using Models in Views students = { 'Sarath':22,

    'Slynux':34 } is static. We can use dynamic content from database from helloweb.student.models import StudentRegister slist = StudentRegister.objects.all() students = {} for s in slist: students[s.name] = s.rollno
  18. Django Workshop Using Models The django python shell $ ./manage

    shell >>> from helloweb.student.models import StudentRegister >>> sobj = StudentRegister() >>> sobj.name = “Slynux” >>> sobj.rollno = 40 >>> sobj.admission_date = '2009-11-01' >>> sobj.save() >>> StudentRegister.objects.all() [<StudentRegister: StudentRegister object>] >>> sob = StudentRegister.objects.get(name='Slynux') >>> sob.delete()
  19. Django Workshop Admin interface Django comes with a builtin Admin

    interface which can be further customized.
  20. Django Workshop Activating Admin settings.py Add 'django.contrib.admin', to INSTALLED_APPS tuple

    urls.py #Uncomment from django.contrib import admin admin.autodiscover() (r'^admin/', include(admin.site.urls)), $ ./manage.py syncdb
  21. Django Workshop Adding Models to Admin Application directory/admin.py from django.contrib

    import admin from myproject.hellweb.models import StudentRegister admin.site.register(StudentRegister)
  22. Django Workshop Hands-on code Writing a Blogging web application •

    Using template.html • Models title,post,category,date • Admin interface • Content search • Filter post by category