SlideShare a Scribd company logo
1 of 32
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

What's hot

SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery EssentialsMark Rackley
 
SPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsSPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsMark Rackley
 
Introduction to using jQuery with SharePoint
Introduction to using jQuery with SharePointIntroduction to using jQuery with SharePoint
Introduction to using jQuery with SharePointRene Modery
 
netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8
netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8
netmind - Primer Contacto con el Desarrollo de Aplicaciones para Windows 8netmind
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011Arun Gupta
 
The SharePoint & jQuery Guide - Updated 1/14/14
The SharePoint & jQuery Guide - Updated 1/14/14The SharePoint & jQuery Guide - Updated 1/14/14
The SharePoint & jQuery Guide - Updated 1/14/14Mark Rackley
 
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint Beast2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint BeastMark Rackley
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Foreverstephskardal
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014Mark Rackley
 
SPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have knownSPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have knownMark Rackley
 
SPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuerySPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQueryMark Rackley
 
Hyperproductive JSF 2.0 @ JavaOne Brazil 2010
Hyperproductive JSF 2.0 @ JavaOne Brazil 2010Hyperproductive JSF 2.0 @ JavaOne Brazil 2010
Hyperproductive JSF 2.0 @ JavaOne Brazil 2010Arun Gupta
 
SharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuerySharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQueryMark Rackley
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile appsIvano Malavolta
 
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...SPTechCon
 
jQuery and AJAX with Rails
jQuery and AJAX with RailsjQuery and AJAX with Rails
jQuery and AJAX with RailsAlan Hecht
 
Integrating Plone with E-Commerce and Relationship Management: A Case Study i...
Integrating Plone with E-Commerce and Relationship Management: A Case Study i...Integrating Plone with E-Commerce and Relationship Management: A Case Study i...
Integrating Plone with E-Commerce and Relationship Management: A Case Study i...David Glick
 

What's hot (20)

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

Similar to Templates

MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2Naji El Kotob
 
Building Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel AppelBuilding Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel Appel.NET Conf UY
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigWake Liu
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction DjangoWade Austin
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code IgniterAmzad Hossain
 
Web Components v1
Web Components v1Web Components v1
Web Components v1Mike Wilcox
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Eric Palakovich Carr
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to TornadoGavin Roy
 
Intro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersIntro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersFrank La Vigne
 
Django workshop : let's make a blog
Django workshop : let's make a blogDjango workshop : let's make a blog
Django workshop : let's make a blogPierre Sudron
 
Modelibra Software Family
Modelibra Software FamilyModelibra Software Family
Modelibra Software Familydzenanr
 

Similar to Templates (20)

MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Building Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel AppelBuilding Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel Appel
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code Igniter
 
Web Components v1
Web Components v1Web Components v1
Web Components v1
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Intro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersIntro to MVC 3 for Government Developers
Intro to MVC 3 for Government Developers
 
Django workshop : let's make a blog
Django workshop : let's make a blogDjango workshop : let's make a blog
Django workshop : let's make a blog
 
Modelibra Software Family
Modelibra Software FamilyModelibra Software Family
Modelibra Software Family
 

More from soon

Deploying
DeployingDeploying
Deployingsoon
 
Google
GoogleGoogle
Googlesoon
 
I18n
I18nI18n
I18nsoon
 
Authentication
AuthenticationAuthentication
Authenticationsoon
 
Admin
AdminAdmin
Adminsoon
 
Files
FilesFiles
Filessoon
 

More from soon (6)

Deploying
DeployingDeploying
Deploying
 
Google
GoogleGoogle
Google
 
I18n
I18nI18n
I18n
 
Authentication
AuthenticationAuthentication
Authentication
 
Admin
AdminAdmin
Admin
 
Files
FilesFiles
Files
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

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

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n