Lecture 21 More On Django.

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

Web Applications Development Using Coldbox Platform Eddie Johnston.
Razor. Slide 2 Remember this? Browser Web Server HTTP Request HTTP Response (Web page / code) Client code (script) Interpret request Generate HTML and.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
DT228/3 Web Development JSP: Directives and Scripting elements.
Struts 2.0 an Overview ( )
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
UNIT-V The MVC architecture and Struts Framework.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Web Development Methodologies Yuan Wang(yw2326). Basic Concepts Browser/Server (B/S) Structure Keywords: Browser, Server Examples: Websites Client/Server.
 >> django-admin.py startproject mysite /mysite __init__.py manage.py settings.py urls.py.
Web Frameworks: Django Department of Biomedical Informatics University of Pittsburgh School of Medicine
Django Web Framework 김형용, 이정민 Framework 2.1. Django High-level Python Web Framework Develop fast Automate the repetitive stuff Follow best practices.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
JavaScript & jQuery the missing manual Chapter 11
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
JSP Java Server Pages Softsmith Infotech.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Lecture 19 Web Application Frameworks Boriana Koleva Room: C54
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Introduction to Applets CS 3505 Client Side Scripting with applets.
HTML. Principle of Programming  Interface with PC 2 English Japanese Chinese Machine Code Compiler / Interpreter C++ Perl Assembler Machine Code.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Tutorial 10 Programming with JavaScript
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introducing ASP.NET 2.0. Internet Technologies WWW Architecture Web Server Client Server Request Response Network HTTP TCP/IP PC/Mac/Unix + Browser (IE,
Java Web Development with NetBeans IDE -- Kai Qian Chapter 5 JavaServer Faces (JSF) Technology.
Topic Java EE installation (Eclipse, glassfish, etc.) Eclipse configuration for EE Creating a Java Web Dynamic Project Creating your first servlet.
Django 101 By: Jason Sumner. Django Overview Django was started in 2003, released under BSD in 2005, and the Django Software Foundation was established.
JavaScript Syntax, how to use it in a HTML document
Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Esri UC 2014 | Technical Workshop | Creating Geoprocessing Services Kevin Hibma.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Simple PHP Web Applications Server Environment
Introduction to MVC Slavomír Moroz. Revision from Previous Lesson o ASP.NET WebForms applications Abstract away HTTP (similar to desktop app development)
Jim Fawcett CSE686 – Internet Programming Spring 2012
Tutorial 10 Programming with JavaScript
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
ASP MVP Web applications and Razor
Play Framework: Introduction
Section 17.1 Section 17.2 Add an audio file using HTML
Haritha Dasari Josue Balandrano Coronel -
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Windows Communication Foundation and Web Services
Intro to PHP & Variables
MVC Framework, in general.
Design and Maintenance of Web Applications in J2EE
Lecture 1: Multi-tier Architecture Overview
In Class Programming BIS1523 – Lecture 11.
WPS - your story so far Seems incredible complicated, already
ASP.NET MVC Web Development
COSC 2956 Internet Tools Java Server Pages.
Struts BY: Tejashri Udavant..
Model View Controller (MVC)
Presentation transcript:

Lecture 21 More On Django

Django Projects and Apps In Django terminology, a "project" is a web environment-the configuration and applications on a particular web site. There may be more than one application in a project. 2

Django Views and Templates Django’s model for rendering web documents is described as a Model-View- Template system. The difference between this and Model-View-Controller is mostly just a difference in terminology. As with MVC, the “Model” consists of the data you are handling in the application. For this part of your code, the fact that you are presenting the application as a web app using Django is not important. This part would be similar for a desktop or mobile application. The model should be completely independent of the UI. The document the user receives is generated by the view, which is a Python code file, *not* an html or xml file. Views, however, typically use templates, which contain html with elements that handle data supplied by the view using a templating language. The basic principle is very similar to Java servlets. 3

Django Views and Templates We must map url patterns to view files so that the application can find the right views. This configuration is done by specifying a) regexes to apply to urls and b) paths to view files in a file named urls.py. from django.conf.urls import url from django.contrib import admin from demo import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.index, name='index'), ] This urls.py works at the project level. On a more complex project, you would need separate urls.py files for each application, which you would reference here using the include statement. 4

Debugging Hints The command line you use to run the development server often shows helpful error messages Django and the development server support hot-swap; you usually do not need to restart the server when you change the code. Hot swap works much better on the development server than in most servers that claim to support it. However, it doesn’t work fif you change url mappings (see below). Also, note that your browser may cache responses, so you may need to reload several times to see changes. 5

Django Views and Templates It is possible to generate a view entirely using Python code. You may have done broadly similar coding using client-side JavaScript. views.py: from django.shortcuts import render from django.http import HttpResponse # Create your views here. def index(request): out = '<h1>Celsius to Fahrenheit Conversions</h1>' for i in range (-40, 101): out += str(i) + ' deg. C' + ' = ' + '{:.1f}'.format(i * 9/5 + 32) + ' deg. F<br />' return HttpResponse(out) 6

Django Views and Templates Views created entirely with Python are hard to debug and edit. It is more productive to handle the html and Python logic separately. Add a line to INSTALLED_APPS in your settings.py file that informs the project about your application. This will be critical for several tasks, including getting it to search for your html templates Put the templates inside the application directory, but in a subdirectory called templates/appname (for example, mysite/demo/templates/demo) 7

Django Views and Templates Templates contain html with additional elements that use a language very much like JSP element syntax for relating application data to html. This document contains reference information for the template tag language: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/ The context is the data applied to the template by the view. In Django, the context is a Python dictionary consisting of variable names and their values. 8

djangodemo/mysite/mysite/urls.py: from django.conf.urls import url from django.contrib import admin from demo import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^conversion', views.conversion, name='conversion'), ] djangodemo/mysite/demo/views.py: from django.shortcuts import render from django.http import HttpResponse def conversion(request): conversions = [] for i in range (-40, 101): curr_tuple = (i, i * 9/5 + 32) conversions.append (curr_tuple) # list of tuples context = { 'conversions': conversions, } return render(request, 'demo/conversion.html', context) 9

djangodemo/mysite/demo/templates/demo/conversion.html: <h1>Celsius to Fahrenheit Conversions</h1> {% if conversions %} {% for pair in conversions %} {{ pair.0 | stringformat:".2f" }} Deg C = {{ pair.1 | stringformat:".2f"}} Deg F<br /> {% endfor %} {% else %} <p>No Data!</p> {% endif %} Part of djangodemo/mysite/mysite/settings.py: INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'demo.apps.DemoConfig', ] 10

Models It’s not a good practice to do calculations in a view. It’s purpose is to provide I/O, and it should not be so tightly coupled to data manipulation. If we were doing something more complicated, we would want the processing thoroughly separated from the GUI, so that we could change either one without touching the other. Revised djangodemo/mysite/demo/models.py : from django.db import models class Converter(models.Model): def convert(self, c): return c * 9/5 + 32 Revised djangodemo/mysite/demo/views.py: from django.shortcuts import render from django.http import HttpResponse from .models import Converter def conversion(request): c = Converter() conversions = [] for i in range (-40, 101): curr_tuple = (i, c.convert(i)) conversions.append (curr_tuple) # list of tuples context = { 'conversions': conversions, } return render(request, 'demo/conversion.html', context) 11

Forms So far, this example has shown output, but not taken any input. For input, we can use html forms. This architecture is not very different from servlets, but is uses different vocabulary: Take user input using a form in a template (not called a “view”). Map the form action to a view function (not called a “controller”) in urls.py View processes data using classes or DB access from the model View creates a context View renders a different template, sending it the context Context presents the new data 12

Forms input.html: <h1>Celsius to Fahrenheit Conversion</h1> <form action="result" method="post"> {% csrf_token %} #https://docs.djangoproject.com/en/1.9/ref/csrf/ <label>Input temperature in Celsius: <input id = "cels_input" name = "cels_input"/></label><br /> <input type="submit" value="Convert" /> </form> 13

Forms result.html: <h1>Celsius to Fahrenheit Conversion Results</h1> {% if result %} {{ result.0 | stringformat:".2f" }} Deg C = {{ result.1 | stringformat:".2f"}} Deg F<br /> {% else %} <p>No Data!</p> {% endif %} 14

Forms views.py: from django.shortcuts import render from django.http import HttpResponse from .models import Converter def input(request): context = {} return render(request, 'demo/input.html', context) def result(request): c = Converter() cels = int(request.POST['cels_input']) result = (cels, c.convert(cels)) context = { 'result': result, } return render(request, 'demo/result.html', context) 15

Forms models.py: from django.db import models class Converter(models.Model): def convert(self, c): return c * 9/5 + 32 djangodemo/mysite/demo/urls.py: from django.conf.urls import url from . import views urlpatterns = [ url(r'^input', views.input, name='input'), url(r'^result', views.result, name='result'), ] 16