SlideShare a Scribd company logo
Model View Controller

      Thierry Sans
Remember index in the Web Directory

                   ... create the corresponding HTML entry in the template


                                                                                Templates

                                                                                <html>
                                                                                <head>
                                                                                    <title> ...
                     webdirectory/
                                           def index(request):
                        <html>
                                                ...
                          <head ...                                     img      name       url


                                            Controller                http://   Khaled   http://

                                                                      http://    Kemal   http://



For each entry in the database ...
                                                                    Database
This is actually a good design



•   Good separation between

    •   The Data Access Model (DAO)

    •   The presentation generating HTTP responses

    •   The business logic handling HTTP requests
Model View Controller (MVC)


•   Model View Controller in Software Engineering

    ➡   Software architecture based on design patterns


            Model                    Data

            View                 Presentation

            Controller          Business Logic
MVC in a web application

                                                      Server Side
Client Side

                                 Controller   Model
              id=scACRSm...


               <html><...


Web Browser                        View

                                                         Database
                                                          Server
                              Web Server
Advantages of MVC in web programming



•   Separation of duties between different experts


    Model                    Data           The database programmer


    View                Presentation        The web designer


    Controller         Business Logic       The web programmer
MVC is a core concept of the web



•   Other MVC web frameworks

    •   Java Servlet and JSP

    •   Ruby on Rails (design principle)

•   But also ...

    •   The new UI, called Metro in Windows 8
Django - Model Template View (MVT)




     Model           Data          Model


      View       Presentation     Template


    Controller   Business Logic    View
Django MVT

                                                        Server Side
Client Side

                               views.py     models.py
              id=scACRSm...


               <html><...


Web Browser                   templates/*

                                                            Database
                                                             Server
Working with templates
Create the controller index


                                               WebDirectory/views.py
  from django.shortcuts import render_to_response
  from WebDirectory.models import Entry


  def index(request):
     entry_list = Entry.objects.all()
      return render_to_response('WebDirectory/index.html',
                                {'entry_list': entry_list})
Create the controller index


                                               WebDirectory/views.py
  from django.shortcuts import render_to_response
  from WebDirectory.models import Entry
                                            Fetch all entries in the database

  def index(request):
     entry_list = Entry.objects.all()
      return render_to_response('WebDirectory/index.html',
                                {'entry_list': entry_list})
Create the controller index


                                                                     WebDirectory/views.py
  from django.shortcuts import render_to_response
  from WebDirectory.models import Entry
                                                                  Fetch all entries in the database

  def index(request):
        entry_list = Entry.objects.all()
        return render_to_response('WebDirectory/index.html',
                                                {'entry_list': entry_list})



 Call the template index.html with the list of all entries as argument
Create the template                WebDirectory/templates/WebDirectory/index.html
<div id="directory">
 {% if entry_list %}
   {% for entry in entry_list %}
         <div class="entry">
            <div class="image"><img src="{{entry.image}}"/>
          </div>
          <div class="name">{{entry.name}}</div>
          <div class="website">
            <a href="{{entry.webpage}}">{{entry.name}}'s website</a>
          </div>
      </div>
   {% endfor %}
{% else %}
   <p>No entry.</p>
{% endif %}
</div>
Modularity
Philosophy




•   Reuse as much code as possible

✓   Easier to maintain

✓   Easier to scale
The index.html template
<div id="directory">               WebDirectory/templates/WebDirectory/index.html
 {% if entry_list %}
   {% for entry in entry_list %}
         <div class="entry">
            <div class="image"><img src="{{entry.image}}"/>
          </div>
          <div class="name">{{entry.name}}</div>
          <div class="website">
            <a href="{{entry.webpage}}">{{entry.name}}'s website</a>
          </div>
      </div>
   {% endfor %}
{% else %}
   <p>No entry.</p>
{% endif %}
</div>
The index.html template
<div id="directory">                WebDirectory/templates/WebDirectory/index.html
 {% if entry_list %}
   {% for entry in entry_list %}
         <div class="entry">
            <div class="image"><img src="{{entry.image}}"/>
          </div>
          <div class="name">{{entry.name}}</div>
          <div class="website">
            <a href="{{entry.webpage}}">{{entry.name}}'s website</a>
          </div>
      </div>
   {% endfor %}
{% else %}
   <p>No entry.</p>               This snippet of code can be reused alone
{% endif %}
                                  (when we will use Ajax actually)
</div>
Create the entry.html template

                             WebDirectory/templates/WebDirectory/entry.html


   <div class="entry">
       <div class="image"><img src="{{e.image}}"/>
     </div>
     <div class="name">{{e.name}}</div>
     <div class="website">
       <a href="{{e.webpage}}">{{e.name}}'s website</a>
     </div>
   </div>
Using the include tag

                                   WebDirectory/templates/WebDirectory/index.html
<div id="directory">
 {% if entry_list %}
   {% for entry in entry_list %}
         {% include "WebDirectory/entry.html" with e=entry %}
    {% endfor %}
{% else %}
   <p>No entry.</p>
{% endif %}
</div>
Using the include tag

                                       WebDirectory/templates/WebDirectory/index.html
<div id="directory">
 {% if entry_list %}
   {% for entry in entry_list %}
         {% include "WebDirectory/entry.html" with e=entry %}
    {% endfor %}
{% else %}
   <p>No entry.</p>    include the external template
{% endif %}            entry.html
</div>
Using the include tag

                                       WebDirectory/templates/WebDirectory/index.html
<div id="directory">
 {% if entry_list %}
   {% for entry in entry_list %}
         {% include "WebDirectory/entry.html" with e=entry %}
    {% endfor %}
{% else %}
   <p>No entry.</p>    include the external template
{% endif %}            entry.html
</div>

                               parameters (the context is passed by default)
Different pages - Same skeleton



•   Some pages might share

    •   headers (title, JS and CSS)

    •   page organization (div tags structuring the page)

    •   footers (if any)
Example

•   Let’s separate the index page from the uploader

✓   index.html and uploader.html shares the same structure


                                  headers
                                  page div


                                 content div
Except ...
                                  WebDirectory/templates/WebDirectory/index.html
...
<div id="page">
      <h1>The CS Directory</h1>
      {% block content %}
      <p>This is ... </p>
      <div id="directory">
          {% if entry_list %}
              {% for entry in entry_list %}
                  {% include "WebDirectory/entry.html" %}
              {% endfor %}
          {% else %}
               <p>No entry.</p>
          {% endif %}
   </div>
   {% endblock %}
</div>                            Only this part will be different
...
Using the extend tag
                             WebDirectory/templates/WebDirectory/uploader.html
{% extends "WebDirectory/index.html" %}



{% block content %}
<div id="publisher">
     <form enctype="multipart/form-data" action="add/" method="post">
         <input id="upload" type="file" name="file"/>
         <input type="text" name="name"/>
         <input type="text" name="webpage"/>
     </form>
     <a href="#" onclick="publish();return false;">Publish</a>
</div>
{% endblock %}
Using the extend tag
                             WebDirectory/templates/WebDirectory/uploader.html
{% extends "WebDirectory/index.html" %}

                                Extending the template index.html
{% block content %}
<div id="publisher">
     <form enctype="multipart/form-data" action="add/" method="post">
         <input id="upload" type="file" name="file"/>
         <input type="text" name="name"/>
         <input type="text" name="webpage"/>
     </form>
     <a href="#" onclick="publish();return false;">Publish</a>
</div>
{% endblock %}
Using the extend tag
                             WebDirectory/templates/WebDirectory/uploader.html
{% extends "WebDirectory/index.html" %}

                                Extending the template index.html
{% block content %}
<div id="publisher">
     <form enctype="multipart/form-data" action="add/" method="post">
         <input id="upload" type="file" name="file"/>
         <input type="text" name="name"/>
         <input type="text" name="webpage"/>
     </form>
     <a href="#" onclick="publish();return false;">Publish</a>
</div>
{% endblock %}
                               Redefining the block content
Managing URLs
The problem with URLs



•   We use relative URLs

➡   If the URL dispatcher configuration (urls.py) changes

๏   we must changes the corresponding URLs in our templates

✓   Instead, we should refer to the URL provided by the URL
    dispatcher in our templates
Using the url tag

                                WebDirectory/templates/WebDirectory/index.html


<html>
<head>
   <title>The CS Directory</title>
   <link   href="{% url WebDirectory.views.index %}css/style.css"
           rel="stylesheet" type="text/css" />
   <script src="{% url WebDirectory.views.index %}js/script.js"
           type="text/javascript"></script>




                  Returns the url /WebDirectory/
                  according to Webdirectory/urls.py file
Same here
                             WebDirectory/templates/WebDirectory/uploader.html

{% extends "WebDirectory/index.html" %}


{% block content %}
<div id="publisher">
     <form enctype="multipart/form-data"
           action="{% url WebDirectory.views.add %}" method="post">
         <input id="upload" type="file" name="file"/>
         <input type="text" name="name"/>
         <input type="text" name="webpage"/>
     </form>
     <a href="#" onclick="publish();return false;">Publish</a>
</div>
{% endblock %}

More Related Content

PPTX
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
PDF
Local storage in Web apps
PDF
HTML5 and CSS3 refresher
PPTX
Sightly - Part 2
PPTX
SPSNH 2014 - The SharePoint & jQueryGuide
PPTX
HTL(Sightly) - All you need to know
PDF
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
PPTX
MVC & SQL_In_1_Hour
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Local storage in Web apps
HTML5 and CSS3 refresher
Sightly - Part 2
SPSNH 2014 - The SharePoint & jQueryGuide
HTL(Sightly) - All you need to know
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
MVC & SQL_In_1_Hour

What's hot (20)

PPTX
SharePoint and jQuery Essentials
PPTX
SPTechCon - Share point and jquery essentials
PPTX
Introduction to using jQuery with SharePoint
PDF
netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8
PDF
JavaServer Faces 2.0 - JavaOne India 2011
PPTX
The SharePoint & jQuery Guide - Updated 1/14/14
PPTX
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
PDF
jQuery and Rails: Best Friends Forever
PPTX
SharePoint & jQuery Guide - SPSNashville 2014
PPTX
SPSDenver - SharePoint & jQuery - What I wish I would have known
PPTX
Why Django for Web Development
PPTX
SPTechCon DevDays - SharePoint & jQuery
PDF
Hyperproductive JSF 2.0 @ JavaOne Brazil 2010
PPTX
SharePoint Saturday St. Louis - SharePoint & jQuery
PPTX
The Django Web Application Framework 2
PDF
[2015/2016] Local data storage for web-based mobile apps
PDF
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
PPT
jQuery and AJAX with Rails
PDF
Integrating Plone with E-Commerce and Relationship Management: A Case Study i...
SharePoint and jQuery Essentials
SPTechCon - Share point and jquery essentials
Introduction to using jQuery with SharePoint
netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8
JavaServer Faces 2.0 - JavaOne India 2011
The SharePoint & jQuery Guide - Updated 1/14/14
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
jQuery and Rails: Best Friends Forever
SharePoint & jQuery Guide - SPSNashville 2014
SPSDenver - SharePoint & jQuery - What I wish I would have known
Why Django for Web Development
SPTechCon DevDays - SharePoint & jQuery
Hyperproductive JSF 2.0 @ JavaOne Brazil 2010
SharePoint Saturday St. Louis - SharePoint & jQuery
The Django Web Application Framework 2
[2015/2016] Local data storage for web-based mobile apps
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
jQuery and AJAX with Rails
Integrating Plone with E-Commerce and Relationship Management: A Case Study i...
Ad

Similar to Templates (20)

PPTX
MVC and Razor - Doc. v1.2
PDF
Django at the Disco
PPTX
Building Modern Websites with ASP.NET by Rachel Appel
ZIP
Django at the Disco
ZIP
Django at the Disco
ZIP
Django at the Disco
ZIP
Django at the Disco
PPTX
The Django Web Application Framework 2
PPTX
The Django Web Application Framework 2
PPTX
The Django Web Application Framework 2
KEY
PHPConf-TW 2012 # Twig
KEY
Introduction Django
ODP
Codegnitorppt
PPT
Introduction To Code Igniter
PDF
Web Components v1
PDF
Django Rest Framework and React and Redux, Oh My!
PDF
An Introduction to Tornado
PPTX
Intro to MVC 3 for Government Developers
PDF
Django workshop : let's make a blog
ODP
Modelibra Software Family
MVC and Razor - Doc. v1.2
Django at the Disco
Building Modern Websites with ASP.NET by Rachel Appel
Django at the Disco
Django at the Disco
Django at the Disco
Django at the Disco
The Django Web Application Framework 2
The Django Web Application Framework 2
The Django Web Application Framework 2
PHPConf-TW 2012 # Twig
Introduction Django
Codegnitorppt
Introduction To Code Igniter
Web Components v1
Django Rest Framework and React and Redux, Oh My!
An Introduction to Tornado
Intro to MVC 3 for Government Developers
Django workshop : let's make a blog
Modelibra Software Family
Ad

More from soon (6)

KEY
Deploying
KEY
Google
KEY
I18n
KEY
Authentication
KEY
Admin
KEY
Files
Deploying
Google
I18n
Authentication
Admin
Files

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf

Templates

  • 1. Model View Controller Thierry Sans
  • 2. Remember index in the Web Directory ... create the corresponding HTML entry in the template Templates <html> <head> <title> ... webdirectory/ def index(request): <html> ... <head ... img name url Controller http:// Khaled http:// http:// Kemal http:// For each entry in the database ... Database
  • 3. This is actually a good design • Good separation between • The Data Access Model (DAO) • The presentation generating HTTP responses • The business logic handling HTTP requests
  • 4. Model View Controller (MVC) • Model View Controller in Software Engineering ➡ Software architecture based on design patterns Model Data View Presentation Controller Business Logic
  • 5. MVC in a web application Server Side Client Side Controller Model id=scACRSm... <html><... Web Browser View Database Server Web Server
  • 6. Advantages of MVC in web programming • Separation of duties between different experts Model Data The database programmer View Presentation The web designer Controller Business Logic The web programmer
  • 7. MVC is a core concept of the web • Other MVC web frameworks • Java Servlet and JSP • Ruby on Rails (design principle) • But also ... • The new UI, called Metro in Windows 8
  • 8. Django - Model Template View (MVT) Model Data Model View Presentation Template Controller Business Logic View
  • 9. Django MVT Server Side Client Side views.py models.py id=scACRSm... <html><... Web Browser templates/* Database Server
  • 11. Create the controller index WebDirectory/views.py from django.shortcuts import render_to_response from WebDirectory.models import Entry def index(request): entry_list = Entry.objects.all() return render_to_response('WebDirectory/index.html', {'entry_list': entry_list})
  • 12. Create the controller index WebDirectory/views.py from django.shortcuts import render_to_response from WebDirectory.models import Entry Fetch all entries in the database def index(request): entry_list = Entry.objects.all() return render_to_response('WebDirectory/index.html', {'entry_list': entry_list})
  • 13. Create the controller index WebDirectory/views.py from django.shortcuts import render_to_response from WebDirectory.models import Entry Fetch all entries in the database def index(request): entry_list = Entry.objects.all() return render_to_response('WebDirectory/index.html', {'entry_list': entry_list}) Call the template index.html with the list of all entries as argument
  • 14. Create the template WebDirectory/templates/WebDirectory/index.html <div id="directory"> {% if entry_list %} {% for entry in entry_list %} <div class="entry"> <div class="image"><img src="{{entry.image}}"/> </div> <div class="name">{{entry.name}}</div> <div class="website"> <a href="{{entry.webpage}}">{{entry.name}}'s website</a> </div> </div> {% endfor %} {% else %} <p>No entry.</p> {% endif %} </div>
  • 16. Philosophy • Reuse as much code as possible ✓ Easier to maintain ✓ Easier to scale
  • 17. The index.html template <div id="directory"> WebDirectory/templates/WebDirectory/index.html {% if entry_list %} {% for entry in entry_list %} <div class="entry"> <div class="image"><img src="{{entry.image}}"/> </div> <div class="name">{{entry.name}}</div> <div class="website"> <a href="{{entry.webpage}}">{{entry.name}}'s website</a> </div> </div> {% endfor %} {% else %} <p>No entry.</p> {% endif %} </div>
  • 18. The index.html template <div id="directory"> WebDirectory/templates/WebDirectory/index.html {% if entry_list %} {% for entry in entry_list %} <div class="entry"> <div class="image"><img src="{{entry.image}}"/> </div> <div class="name">{{entry.name}}</div> <div class="website"> <a href="{{entry.webpage}}">{{entry.name}}'s website</a> </div> </div> {% endfor %} {% else %} <p>No entry.</p> This snippet of code can be reused alone {% endif %} (when we will use Ajax actually) </div>
  • 19. Create the entry.html template WebDirectory/templates/WebDirectory/entry.html <div class="entry"> <div class="image"><img src="{{e.image}}"/> </div> <div class="name">{{e.name}}</div> <div class="website"> <a href="{{e.webpage}}">{{e.name}}'s website</a> </div> </div>
  • 20. Using the include tag WebDirectory/templates/WebDirectory/index.html <div id="directory"> {% if entry_list %} {% for entry in entry_list %} {% include "WebDirectory/entry.html" with e=entry %} {% endfor %} {% else %} <p>No entry.</p> {% endif %} </div>
  • 21. Using the include tag WebDirectory/templates/WebDirectory/index.html <div id="directory"> {% if entry_list %} {% for entry in entry_list %} {% include "WebDirectory/entry.html" with e=entry %} {% endfor %} {% else %} <p>No entry.</p> include the external template {% endif %} entry.html </div>
  • 22. Using the include tag WebDirectory/templates/WebDirectory/index.html <div id="directory"> {% if entry_list %} {% for entry in entry_list %} {% include "WebDirectory/entry.html" with e=entry %} {% endfor %} {% else %} <p>No entry.</p> include the external template {% endif %} entry.html </div> parameters (the context is passed by default)
  • 23. Different pages - Same skeleton • Some pages might share • headers (title, JS and CSS) • page organization (div tags structuring the page) • footers (if any)
  • 24. Example • Let’s separate the index page from the uploader ✓ index.html and uploader.html shares the same structure headers page div content div
  • 25. Except ... WebDirectory/templates/WebDirectory/index.html ... <div id="page"> <h1>The CS Directory</h1> {% block content %} <p>This is ... </p> <div id="directory"> {% if entry_list %} {% for entry in entry_list %} {% include "WebDirectory/entry.html" %} {% endfor %} {% else %} <p>No entry.</p> {% endif %} </div> {% endblock %} </div> Only this part will be different ...
  • 26. Using the extend tag WebDirectory/templates/WebDirectory/uploader.html {% extends "WebDirectory/index.html" %} {% block content %} <div id="publisher"> <form enctype="multipart/form-data" action="add/" method="post"> <input id="upload" type="file" name="file"/> <input type="text" name="name"/> <input type="text" name="webpage"/> </form> <a href="#" onclick="publish();return false;">Publish</a> </div> {% endblock %}
  • 27. Using the extend tag WebDirectory/templates/WebDirectory/uploader.html {% extends "WebDirectory/index.html" %} Extending the template index.html {% block content %} <div id="publisher"> <form enctype="multipart/form-data" action="add/" method="post"> <input id="upload" type="file" name="file"/> <input type="text" name="name"/> <input type="text" name="webpage"/> </form> <a href="#" onclick="publish();return false;">Publish</a> </div> {% endblock %}
  • 28. Using the extend tag WebDirectory/templates/WebDirectory/uploader.html {% extends "WebDirectory/index.html" %} Extending the template index.html {% block content %} <div id="publisher"> <form enctype="multipart/form-data" action="add/" method="post"> <input id="upload" type="file" name="file"/> <input type="text" name="name"/> <input type="text" name="webpage"/> </form> <a href="#" onclick="publish();return false;">Publish</a> </div> {% endblock %} Redefining the block content
  • 30. The problem with URLs • We use relative URLs ➡ If the URL dispatcher configuration (urls.py) changes ๏ we must changes the corresponding URLs in our templates ✓ Instead, we should refer to the URL provided by the URL dispatcher in our templates
  • 31. Using the url tag WebDirectory/templates/WebDirectory/index.html <html> <head> <title>The CS Directory</title> <link href="{% url WebDirectory.views.index %}css/style.css" rel="stylesheet" type="text/css" /> <script src="{% url WebDirectory.views.index %}js/script.js" type="text/javascript"></script> Returns the url /WebDirectory/ according to Webdirectory/urls.py file
  • 32. Same here WebDirectory/templates/WebDirectory/uploader.html {% extends "WebDirectory/index.html" %} {% block content %} <div id="publisher"> <form enctype="multipart/form-data" action="{% url WebDirectory.views.add %}" method="post"> <input id="upload" type="file" name="file"/> <input type="text" name="name"/> <input type="text" name="webpage"/> </form> <a href="#" onclick="publish();return false;">Publish</a> </div> {% endblock %}

Editor's Notes