SlideShare a Scribd company logo
django

python powered MVC framework
Presentation topics

● why python and why django?
● what is MVC?
● the M in the django framework
● the V in the django framework
● and the C in the django framework
● django admin
why python and why django?

#1, why python?
 ● python is a "natural" OO language;
 ● python has functional capabilities;
 ● "batteries included"!!

#2, why django?
 ● uses python (ahhh!);
 ● interfaces for common actions (dry);
 ● pluggable apps;
 ● you have more time to CREATE!
 ● you had VALUE to you products;
what is MVC?

A software development pattern where the business logic is
separated from the UI.

In web development:

 ● (M)odel: the business logic/rules off the application (mostly
   the DB);

 ● (V)iew: the (x)html rendered and presented to the user;

 ● (C)ontroller: the handler that receives GET or POST
   requests, works over the domain object and retrieves a
   response to the user;
what is MVC?

In the Django framework, things are a little bit different...



                      Welcome to "MTV"!!
                    (Model - Template - View)
the M in the django framework
the M in the django framework

Not much different from the other M...

 ● It holds the business logic/rules;

 ● It is related with the DB;

 ● Describes the DB as classes of objects using a mapper
   (ORM);

 ● It holds methods to retrieve data;
the M in the django framework
Example:
from django.db import models

class Company(models.Model):
    name = models.CharField(_("Company name"), max_length=64)
    phone = models.CharField("URL", max_length=32)
    fax = models.CharField("Fax", max_length=32, null=True, blank=True)
    email = models.EmailField("E-mail", max_length=32, null=True, blank=True)
    ceos = models.ForeignKey('entities.Ceo')

    class Meta:
        ordering = ['name']
        db_table = 'company'
        verbose_name = _('Company')
        verbose_name_plural = _('Companies')

    # Return company's administrators
    def get_ceos(self):
        return self.ceos

    # Returns an instance in a human-readable format
    def __unicode__(self):
        return self.name
the V in the django framework
the V in the django framework

A view, in django, has two parts:

#1 A callback that:

 ● is a normal python method that always receive an HTTP
   request object...

 ● ... and always returns an HTTP response object;

 ● describes which data is presented;
the V in the django framework

#2 An URLconf:

 ● that is a mapper between url's and the view callbacks;

 ● that is described by regexes;
the V in the django framework

We can also include the templates here, since it is intrinsically
linked to the view callbacks.

 ● Django has its own template system/language;

 ● It is pythonic, but very stricted;

 ● Organized by tags;

 ● Focus on how you display data;

 ● You can write your own set of tags;
the V in the django framework
Example of a view:
from django.shortcuts import render_to_response
def property_details(request, id):
   try:
      c = {}
      c.update(csrf(request))
      property = Property.objects.get(pk=id)
      c['property'] = property
   except Property.DoesNotExist:
      raise Http404
   return render_to_response('properties/property_details.html', c)

Example of an URLconf:
urlpatterns = patterns('properties.views',
   (r'^(?P<id>d+[a-z]*)', 'property_details'),
)
the V in the django framework

Example of a template:
{% extends "base.html" %}
{% load i18n %}

{% block highlights %}
{% include "horizontal_search.html" %}
<div style="margin-top: 25px;"></div>
{% endblock %}
{% block content %}
  <div style="padding-left: 9px; padding-right: 6px;">
  <div id="text_wrapper">
<h4>{% trans "About" %} {{ property.reference }}</h4>
{{ property.description }}
  </div>
{% endblock %}
the C in the django framework
the C in the django framework
Possibly the most complex part... Basically it's all the django
framework! This includes:

 ● modules that are responsible for user authentication...

 ● modules that prevents CSRF's...

 ● that are responsible for the website language (i18n, l7n);

 ● etc...

It's all the machinery between an URLconf and a view. Those
modules are called middleware.
django admin
django admin

django admin is a very usefull app that allows you to save a lot
of time.

 ● Uses class attributes to create forms;

 ● Common methods included (add, edit, remove);

 ● Complete admin interface that can be changed;

 ● User management;
django admin
Default objects list
django admin
Default add form

More Related Content

ODP
* DJANGO - The Python Framework - Low Kian Seong, Developer
KEY
Dojango
PDF
Building Pluggable Web Applications using Django
PDF
A Basic Django Introduction
ODP
Django tech-talk
PDF
Slides
PDF
Django Overview
PDF
* DJANGO - The Python Framework - Low Kian Seong, Developer
Dojango
Building Pluggable Web Applications using Django
A Basic Django Introduction
Django tech-talk
Slides
Django Overview

What's hot (20)

PPTX
Introduction to django
PPTX
Django - Python MVC Framework
PDF
Automation - fabric, django and more
PPTX
Introduction to Django
ODP
Django Seminar 08/17/2013
PDF
Django Best Practices
PDF
App Engine
PPT
Django
PPT
Agile Development With Hobo
PPTX
Best Tools for first time Odoo Development
PPT
Create a web-app with Cgi Appplication
PDF
django
PDF
PyCon Korea 2019 REST API Document Generation
PDF
Getting big without getting fat, in perl
PPT
PPTX
Design patterns as power of programing
PPT
J Query
PDF
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
PDF
Getting modern with logging via log4perl
PDF
Efficient Django
Introduction to django
Django - Python MVC Framework
Automation - fabric, django and more
Introduction to Django
Django Seminar 08/17/2013
Django Best Practices
App Engine
Django
Agile Development With Hobo
Best Tools for first time Odoo Development
Create a web-app with Cgi Appplication
django
PyCon Korea 2019 REST API Document Generation
Getting big without getting fat, in perl
Design patterns as power of programing
J Query
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Getting modern with logging via log4perl
Efficient Django
Ad

Similar to A gentle intro to the Django Framework (20)

PPTX
Web development with django - Basics Presentation
PPTX
PDF
Introduction to Django
PDF
Google App Engine in 40 minutes (the absolute essentials)
PDF
GDG Addis - An Introduction to Django and App Engine
ODP
TangoWithDjango - ch8
PPTX
Django Frequently Asked Interview Questions
PDF
django_introduction20141030
PPTX
Discovering Django - zekeLabs
PDF
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
PDF
Scalable web application architecture
PDF
gDayX - Advanced angularjs
PPTX
Tango with django
PDF
Django cheat sheet
PDF
Introduction to angular js
PPTX
learnpythondjangochapteroneintroduction.pptx
PDF
AngularJS Workshop
PPT
Mini Curso Django Ii Congresso Academico Ces
PDF
بررسی چارچوب جنگو
PDF
Introduction to django
Web development with django - Basics Presentation
Introduction to Django
Google App Engine in 40 minutes (the absolute essentials)
GDG Addis - An Introduction to Django and App Engine
TangoWithDjango - ch8
Django Frequently Asked Interview Questions
django_introduction20141030
Discovering Django - zekeLabs
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
Scalable web application architecture
gDayX - Advanced angularjs
Tango with django
Django cheat sheet
Introduction to angular js
learnpythondjangochapteroneintroduction.pptx
AngularJS Workshop
Mini Curso Django Ii Congresso Academico Ces
بررسی چارچوب جنگو
Introduction to django
Ad

A gentle intro to the Django Framework

  • 2. Presentation topics ● why python and why django? ● what is MVC? ● the M in the django framework ● the V in the django framework ● and the C in the django framework ● django admin
  • 3. why python and why django? #1, why python? ● python is a "natural" OO language; ● python has functional capabilities; ● "batteries included"!! #2, why django? ● uses python (ahhh!); ● interfaces for common actions (dry); ● pluggable apps; ● you have more time to CREATE! ● you had VALUE to you products;
  • 4. what is MVC? A software development pattern where the business logic is separated from the UI. In web development: ● (M)odel: the business logic/rules off the application (mostly the DB); ● (V)iew: the (x)html rendered and presented to the user; ● (C)ontroller: the handler that receives GET or POST requests, works over the domain object and retrieves a response to the user;
  • 5. what is MVC? In the Django framework, things are a little bit different... Welcome to "MTV"!! (Model - Template - View)
  • 6. the M in the django framework
  • 7. the M in the django framework Not much different from the other M... ● It holds the business logic/rules; ● It is related with the DB; ● Describes the DB as classes of objects using a mapper (ORM); ● It holds methods to retrieve data;
  • 8. the M in the django framework Example: from django.db import models class Company(models.Model): name = models.CharField(_("Company name"), max_length=64) phone = models.CharField("URL", max_length=32) fax = models.CharField("Fax", max_length=32, null=True, blank=True) email = models.EmailField("E-mail", max_length=32, null=True, blank=True) ceos = models.ForeignKey('entities.Ceo') class Meta: ordering = ['name'] db_table = 'company' verbose_name = _('Company') verbose_name_plural = _('Companies') # Return company's administrators def get_ceos(self): return self.ceos # Returns an instance in a human-readable format def __unicode__(self): return self.name
  • 9. the V in the django framework
  • 10. the V in the django framework A view, in django, has two parts: #1 A callback that: ● is a normal python method that always receive an HTTP request object... ● ... and always returns an HTTP response object; ● describes which data is presented;
  • 11. the V in the django framework #2 An URLconf: ● that is a mapper between url's and the view callbacks; ● that is described by regexes;
  • 12. the V in the django framework We can also include the templates here, since it is intrinsically linked to the view callbacks. ● Django has its own template system/language; ● It is pythonic, but very stricted; ● Organized by tags; ● Focus on how you display data; ● You can write your own set of tags;
  • 13. the V in the django framework Example of a view: from django.shortcuts import render_to_response def property_details(request, id): try: c = {} c.update(csrf(request)) property = Property.objects.get(pk=id) c['property'] = property except Property.DoesNotExist: raise Http404 return render_to_response('properties/property_details.html', c) Example of an URLconf: urlpatterns = patterns('properties.views', (r'^(?P<id>d+[a-z]*)', 'property_details'), )
  • 14. the V in the django framework Example of a template: {% extends "base.html" %} {% load i18n %} {% block highlights %} {% include "horizontal_search.html" %} <div style="margin-top: 25px;"></div> {% endblock %} {% block content %} <div style="padding-left: 9px; padding-right: 6px;"> <div id="text_wrapper"> <h4>{% trans "About" %} {{ property.reference }}</h4> {{ property.description }} </div> {% endblock %}
  • 15. the C in the django framework
  • 16. the C in the django framework Possibly the most complex part... Basically it's all the django framework! This includes: ● modules that are responsible for user authentication... ● modules that prevents CSRF's... ● that are responsible for the website language (i18n, l7n); ● etc... It's all the machinery between an URLconf and a view. Those modules are called middleware.
  • 18. django admin django admin is a very usefull app that allows you to save a lot of time. ● Uses class attributes to create forms; ● Common methods included (add, edit, remove); ● Complete admin interface that can be changed; ● User management;