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

Django — The Web framework for perfectionists w...

Django — The Web framework for perfectionists with deadlines

This talk introduces Django, a Python-based web framework that simplifies the task of building web applications.

Clay McClure

April 20, 2013
Tweet

More Decks by Clay McClure

Other Decks in Programming

Transcript

  1. frame·work n. 1. A structure for supporting or enclosing something

    else. 2. A set of assumptions, concepts, values, and practices that constitutes a way of viewing reality. The American Heritage® Dictionary of the English Language, Fourth Edition copyright ©2000 by Houghton Mifflin Company.
  2. frame·work n. 1. A structure for supporting or enclosing something

    else. 2. A set of conventions that constitutes a way of doing something. The American Heritage® Dictionary of the English Language, Fourth Edition copyright ©2000 by Houghton Mifflin Company.
  3. CGI

  4. MVC

  5. from  django.db  import  models class  Band(models.Model):        name

     =  models.CharField(max_length=100)        year_formed  =  models.IntegerField()        def  make_musical_awesome(self):                #  TODO:  discover  secret  to  success
  6.        name  =  models.CharField(max_length=100)        year_formed

     =  models.IntegerField() from  django.db  import  models class  Band(models.Model):        name  =  models.CharField(max_length=100)        year_formed  =  models.IntegerField()        def  make_musical_awesome(self):                #  TODO:  discover  secret  to  success state
  7.        def  make_musical_awesome(self):          

         #  TODO:  discover  secret  to  success from  django.db  import  models class  Band(models.Model):        name  =  models.CharField(max_length=100)        year_formed  =  models.IntegerField()        def  make_musical_awesome(self):                #  TODO:  discover  secret  to  success behavior
  8. mysql>  DESCRIBE  music_band; +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ |  Field        

         |  Type            |  Null  |  Key  |  Default  |  Extra                    | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+ |  id                    |  int(11)      |  NO      |  PRI  |  NULL        |  auto_increment  | |  name                |  char(100)  |  NO      |          |                  |                                | |  year_formed  |  int(11)      |  NO      |          |                  |                                | +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+
  9. field types AutoField BigIntegerField BinaryField BooleanField CharField DateField DateTimeField DecimalField

    EmailField FileField FilePathField FloatField ImageField IntegerField IPAddressField NullBooleanField PositiveIntegerField SlugField SmallIntegerField TextField TimeField URLField ForeignKey ManyToManyField OneToOneField
  10. INSERT  INTO  music_band  VALUES  ... SELECT  *  FROM  music_band  WHERE

     ... UPDATE  music_band  SET  ... DELETE  FROM  music_band  WHERE  ...
  11. INSERT  INTO  music_band  VALUES  ... SELECT  *  FROM  music_band  WHERE

     ... UPDATE  music_band  SET  ... DELETE  FROM  music_band  WHERE  ...
  12. Band.objects.create(        name="RUSH",        year_formed=1968) Band.objects.create(

           name="Led  Zeppelin",        year_formed=1968) Band.objects.create(        name="The  Police",        year_formed=1977)
  13. from  django.db  import  models class  Band(models.Model):        name

     =  models.CharField(max_length=100)        year_formed  =  models.IntegerField()        members  =  models.ManyToManyField("Musician") class  Musician(models.Model):        name  =  models.CharField(max_length=100)
  14. bands  =  Band.objects.filter(year_formed=1968)\              

                             .exclude(name="RUSH")\                                        .order_by("name") Chain
  15. chainable methods filter exclude annotate order_by reverse distinct values values_list

    dates datetimes none all select_related prefetch_related extra defer only using select_for_update
  16. if  Band.objects.filter(year_formed=1968)\                

                 .exclude(name="RUSH")\                              .exists():        #  do  something num  =  Band.objects.filter(year_formed=1968)\                                    .exclude(name="RUSH")\                                    .count() print  num
  17. Band.objects.filter(year_formed=1968,                  

                         name="RUSH") field lookups {
  18. field lookups exact iexact contains icontains in gt gte lt

    lte startswith istartswith endswith iendswith range year month day week_day hour minute second isnull search regex iregex
  19. class  Band(models.Model):        name  =  models.CharField(max_length=100)    

       year_formed  =  models.IntegerField()        members  =  models.ManyToManyField("Musician") class  Musician(models.Model):        name  =  models.CharField(max_length=100) Band.objects.filter(members__name="Keith  Moon")
  20. <html>    <body>        <ul>      

         {%  for  band  in  bands  %}                <li>                    {{  band.name  }}                    has  been  rocking  since                    {{  band.year_formed  }}.                </li>            {%  endfor  %}        </ul>    </body> </html>
  21. URL path request.path /say/hello HTTP method request.method GET Query string

    request.GET foo=bar&blee=baz Form data request.POST <form method="POST"> Cookies request.COOKIES key=value HTTP headers request.META Content-type: text/html Logged-in user request.user.name Keyser Söze
  22. from  django.shortcuts  import  render_to_response def  hello(request):        context

     =  {                "name":  "Pete",        }        template  =  "hello.html"        return  render_to_response(template,  context)
  23. from  django.shortcuts  import  get_object_or_404 from  django.shortcuts  import  render_to_response def  hello(request):

           band  =  get_object_or_404(Band,  name="RUSH")        context  =  {                "band":  band,        }        template  =  "hello.html"        return  render_to_response(template,  context)
  24. but that's not all... forms middleware context processors signals migrations

    admin site authentication messages redirects site maps sessions static assets