Web Frameworks: Django Department of Biomedical Informatics University of Pittsburgh School of Medicine

Slides:



Advertisements
Similar presentations
4.01 How Web Pages Work.
Advertisements

Objective Understand web-based digital media production methods, software, and hardware. Course Weight : 10%
Server-Side vs. Client-Side Scripting Languages
DT228/3 Web Development multi page applications/ sharing data.
Input Validation For Free Text Fields ADD Project Members: Hagar Offer & Ran Mor Academic Advisor: Dr Gera Weiss Technical Advisors: Raffi Lipkin & Nadav.
Chapter Apache Installation in Linux- Mandrake. Acknowledgment The following information has been obtained directly from
Week 2 IBS 685. Static Page Architecture The user requests the page by typing a URL in a browser The Browser requests the page from the Web Server The.
(NHA) The Laboratory of Computer Communication and Networking Network Host Analyzer.
Python and Web Programming
Direct Congress Dan Skorupski Dan Vingo. Inner workings Reminder: MVC design pattern Procedural view: From request to response o Request passed to a view.
8/6/2015Auto Attendants 1 Smarter Communications.
How to Establish a Blog. What is a Blog A blog is a collection of informational articles/ideas intended to update a viewer on new information associated.
Dynamic Web site With PHP and MySQL. MySQL The combination of MySQL database and PHP scripting language is optimum for building dynamic websites. MySQL.
The Internet & The World Wide Web Notes
Hands-On Microsoft Windows Server 2008 Chapter 8 Managing Windows Server 2008 Network Services.
Linux Operations and Administration
Web Development Methodologies Yuan Wang(yw2326). Basic Concepts Browser/Server (B/S) Structure Keywords: Browser, Server Examples: Websites Client/Server.
Django Web Framework 김형용, 이정민 Framework 2.1. Django High-level Python Web Framework Develop fast Automate the repetitive stuff Follow best practices.
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Dj(T)ango with Python Ritika Virmani. What is Django? It’s not a Hawaiian dance Developed by Adrian Holovaty and Simon Willison Rapid Web Development.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Chapter 1: Introduction to Web
Copyright© 2003 Avaya Inc. All rights reserved Upgrade to Communication Manager 2.0 with Migration to Linux 8.0 Purpose: This presentation was prepared.
Lecturer: Ghadah Aldehim
Chapter 16 The World Wide Web. 2 The Web An infrastructure of information combined and the network software used to access it Web page A document that.
Introducing Dreamweaver MX 2004
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
Creating a Web Site to Gather Data and Conduct Research.
ASP.NET Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Web Design and Development for E-Business By Jensen J. Zhao Copyright 2003 Prentice Hall, Inc. Web Design and Development for E-Business Jensen J. Zhao.
Universiti Utara Malaysia Chapter 3 Introduction to ASP.NET 3.5.
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.
Unit 2, cont. September 12 More HTML. Attributes Some tags are modifiable with attributes This changes the way a tag behaves Modifying a tag requires.
Who uses it? MichaelMoore.com What's it all about? Rapid Development Clean, Pragmatic Design.
1 Chinese Information Processing : Using Computers to Teach and Learn Chinese Week 6 and 7: Creating and maintaining web pages - html and ftp.
Django 101 By: Jason Sumner. Django Overview Django was started in 2003, released under BSD in 2005, and the Django Software Foundation was established.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
1 UNIT 13 The World Wide Web Lecturer: Kholood Baselm.
Chapter 4: Working with ASP.NET Server Controls OUTLINE  What ASP.NET Server Controls are  How the ASP.NET run time processes the server controls on.
Introduction to JavaScript CS101 Introduction to Computing.
HTML BTEC National in Computing Section5. Create Information “HTML: defining HTML, discussing HTML uses and demonstrating HTML basics, HTML structure…..
Table of Contents TopicSlide Administrator Login 2 Administrator Navigations 3 Managing AlternativeDr.com Blogs 4 Managing Dr. Lloyd May Blogs 5 Managing.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Tomcat Setup BCIS 3680 Enterprise Programming. One-Click Tomcat Setup 2  This semester we’ll try to set up Tomcat with a PowerShell script.  Preparation.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Introduction. Internet Worldwide collection of computers and computer networks that link people to businesses, governmental agencies, educational institutions,
1 UNIT 13 The World Wide Web. Introduction 2 Agenda The World Wide Web Search Engines Video Streaming 3.
1 UNIT 13 The World Wide Web. Introduction 2 The World Wide Web: ▫ Commonly referred to as WWW or the Web. ▫ Is a service on the Internet. It consists.
Live. learn. work. play Superior Avenue Suite 310 Cleveland Ohio Tel: Fax:
Joomla Awdhesh Kumar Singsys Pte Ltd. What is Joomla? Joomla is an award-winning content management system (CMS), which enables you to build Web sites.
How to use Drupal Awdhesh Kumar (Team Leader) Presentation Topic.
Development Environment
Project Management: Messages
INSTALLING AND SETTING UP APACHE2 IN A LINUX ENVIRONMENT
Django in the real world
Objective Understand web-based digital media production methods, software, and hardware. Course Weight : 10%
Lecture 21 More On Django.
Presentation transcript:

Web Frameworks: Django Department of Biomedical Informatics University of Pittsburgh School of Medicine

The Purpose of the Lecture Introduce the basic steps for web programming using Django. This is done by walking you though the steps that takes to create an web blog: “jiangblog” “jiangblog” is designed to allow a user to make comments online about some topics such as a presidential debate, and save these comments in a database.

Python and SQLite has to be installed before installing django!

Download and Install Django 1.Download Django from the following website: 2. Install Django: tar xzvf Django tar.gz cd Django sudo python setup.py install

Create a Django Project Django provides a utility called django-admin.py that can streamline the tasks for you when you create a django project. (Note that I use red color for the terminal output.) xjiang-lp:django xij6$ django-admin.py startproject jiangblog xjiang-lp:django xij6$ ls -l total 0 drwxr-xr-x 4 xij6 PITT\domain users 136 Nov 8 11:28 jiangblog drwxr-xr-x 7 xij6 PITT\domain users 238 Nov 8 11:05 mysite

Check What have been Created in jiangblog xjiang-lp:django xij6$ cd jiangblog xjiang-lp:jiangblog xij6$ ls -l total 8 drwxr-xr-x 6 xij6 PITT\domain users 204 Nov 8 11:28 jiangblog -rwxr-xr-x 1 xij6 PITT\domain users 252 Nov 8 11:28 manage.py xjiang-lp:jiangblog xij6$

Check What have been Created in jiangblog xjiang-lp:jiangblog xij6$ ls -l jiangblog total 32 -rw-r--r-- 1 xij6 PITT\domain users 0 Nov 8 11:28 __init__.py -rw-r--r-- 1 xij6 PITT\domain users 5221 Nov 8 11:28 settings.py -rw-r--r-- 1 xij6 PITT\domain users 565 Nov 8 11:28 urls.py -rw-r--r-- 1 xij6 PITT\domain users 1140 Nov 8 11:28 wsgi.py

The Files Created by Django-Admin.py startproject manage.py: Command-line interface for applications. __init__.py: Specifies to Python that this is a package. urls.py: Global URL configuration (“URLconf”). settings.py: Project-specific configuration wsgi.py: ???

The “pure Python” philosophy Note that every file created by the startproject command is Python code with an extension.py Django tries to stay with Python code wherever possible. This certainly reduces the complexity to the framework for a python programmer.

Running the Development Server The development server is a server designed for the development phase that runs on your local computer. xjiang-lp:jiangblog xij6$ python./manage.py runserver Validating models… 0 errors found Django version 1.4.2, using settings 'jiangblog.settings' Development server is running at Quit the server with CONTROL-C. Error: [Errno 48] Address already in use

Running the Development Server Note that if you want to run your server on a different port, you can specify that on the command line: python./manage.py runserver

xjiang-lp:jiangblog xij6$./manage.py runserver 8080 Validating models... 0 errors found Django version 1.4.2, using settings 'jiangblog.settings' Development server is running at Quit the server with CONTROL-C.

DBMIAdmins-MacBook-Pro:jiangblog xij6$./manage.py startapp presdebate DBMIAdmins-MacBook-Pro:jiangblog xij6$ ls -l total 8 drwxr-xr-x 10 xij6 PITT\domain users 340 Nov 8 12:32 jiangblog -rwxr-xr-x 1 xij6 PITT\domain users 252 Nov 8 11:28 manage.py drwxr-xr-x 6 xij6 PITT\domain users 204 Nov 8 13:33 presdebate DBMIAdmins-MacBook-Pro:jiangblog xij6$

Create an Application within a Project We want to create an application named as “presdebate”, which can be used to collect comments from web users about a presidential debate../manage.py startapp presdebate

Again, we want to know what files have been created for our application. DBMIAdmins-MacBook-Pro:jiangblog xij6$ ls -l presdebate total 24 -rw-r--r-- 1 xij6 PITT\domain users 0 Nov 8 13:33 __init__.py -rw-r--r-- 1 xij6 PITT\domain users 57 Nov 8 13:33 models.py -rw-r--r-- 1 xij6 PITT\domain users 383 Nov 8 13:33 tests.py -rw-r--r-- 1 xij6 PITT\domain users 26 Nov 8 13:33 views.py DBMIAdmins-MacBook-Pro:jiangblog xij6$

The Files Created for our Application “presdebate” __init__.py: Specifies to Python that this is a package models.py: Data models views.py: view functions tests.py: unit tests What is missing? urls.py: The app’s URL configuration (“URLconf”). This is not automatically created.

To inform Django that the new app “presdebate” is part of our project, we need to edit settings.py. We need to open the settings.py with an editor and find the INSTALLED_APPS tuple near the bottom, then add our application name (presdebate) as a member of that tuple. In Mac I typically use either textmate or coda2 to do the editing work.

NSTALLED_APPS after editing

Creating a Data Model We need to create a data model via editing the file called models.py, which is automatically generated for application presdebate. This data model defines the data structures of presdebate. Django provides a variety of fields that map to our application data. In application presdebate, we will use three different field types.

Models.py before editing

Models.py after editing

The newly created class, PresDebate, is a subclass of django.db.models.Model. That’s Django standard base class for data models, which is the core of Django’s powerful ORM. The fields are defined like regular class attributes, with each one being an instance of a particular field class.

A CharField is appropriate for short, single lines of text. A TextField type is good for a large chunk of text, such as a large paragraph of comments we may collect from a user. A DateTimeField is represented by a Python datetime.datetime object. For a list of field types provided by Django, refer to the documentation at: ld-types

Setting Up the Database Using SQLite with the local host, you only need to do two things: 1.Update the engine. 2. Specify the name (use full pathname to avoid confusion) of the database that will be created in the settings.py file.

DATABASES entry in settings.py before editing

“DATABASES”entry in settings.py after editing

Setting Up the Database Now we need to tell Django to use the information (the pathname of our database) you’ve given it to connect to the database and set up tables that our application needs. We use the syncdb command of manage.py to do this.

Setting Up the Database When the syncdb command is issued, Django looks for a models.py file in each of the INSTALLED_APPS entry. For each model it finds, it creates a database table. Adding a super user is useful, especially when we add the Django’s automatic admin application later.

Creating the predebate’s User Interface With Django, a Web page has the following typical components: 1.A template that defines how it looks, and displays information passed to it. 2. A view function that performs the core logic for a request, fetch the information to be displayed from a database, and store new information entered by a users to the database.

Creating the Predebate’s User Interface 3. A URL pattern that matches an incoming request with the corresponding view. We will build our application according to the following order: 1.Build a template. 2.Design an Url Pattern. 3.Develop the view function.

Creating a Django Template We will introduce two constructs of django’s templating language: 1.A variable tag that is delimited by pairs of curly braces: {{ … }}. A variable tag displays the contents of the object within the braces, which can be pure data or callables. Inside a variable tag, you can use Python-style dot- notation to access attributes of the tag variables.

Creating a Django Template 2.A block tag that is delimited by curly braces and percent symbols : {% … %}. A block tag is used to embed logic such as loops and conditions into a django html template.

A Preliminary Template Created for presdebate Application

Creating a Django Template Save the HTML template code shown in the previous slide into a directory called templates inside the application folder, which we need to create. So the path to the template we just created is: /users/xij6/django/jiangblog/presdebate/templates/mytempl ate.html Note that the name of the template is arbitrary, but the name of the templates directory is mandatory.

Creating a URL Pattern The pathnames of an URL in a users’ browsers are mapped to various parts of an applicaton. The IP address will be mapped to an Internet host (server), and the port number will be mapped to a port on the server. Once the message arrives at the Internet host, the serve will match the remainder of the URL to the corresponding project. Then the remainder of the URL (with the project name excluded) will be passed to the project url configuration file.

Creating a URL Pattern In our case in which we use a local host as the server, the type of request and the remainder of the URL (with the IP and port number excluded) will be passed to a django project URLconf (jiangblog/urls.py). We therefore need to edit the URLconf so that when a valid pattern (regular expression) matches to the remainder of the URL received, the request will be further passed to the next receiver, which is the application URLconf we shell create later.

jiangblog/urls.py before editing

jiangblog/urls.py after editing

Creating a URL Pattern The patterns() function takes as input a tuple (URL regular expression, destination). The destination is either a view function that is called for URLs that match the pattern, or it’s a call to another URLconf file via include() function. When include() is used, the current URL path head is removed and the remainder of the path is passed to the another URLconf file.

Creating a URL Pattern For example, for the xxx/yyy/zzz will be passed to presdebate/urls.py as specified by include (“presdebate.urls”), by excluding the matching regex presdebate/

Creating a URL Pattern The added line in the urlpattens entry of the project URLconf says that we are catching requests that begin with presdebate/ and passing them on to the presdebate/urls.py. Recall that the presdebate does not contain a urls.py, thus we need to create this file from scratch.

presdebate/urls.py

Creating a View Function-Create a Fake View First The purpose of creating a fake view is to test quickly whether we’ve done so fast are correct. To do this, we need to edit the jiangblog/presdebate/view.py file.

jiangblog/presdebate/view.py before editing

jiangblog/presdebate/view.py after editing

Creating Our Real View Function Now we will create a view function that will fetch all of the comments stored in the database (mydb.db)and display them to users via our template.

jiangblog/presdebate/view.py after editing

Creating Our Real View Function If there is no records in mydb.db, we will receive an error. Thus, we need to initialize the mydb.db. This can be done in several ways. One way is to add a review function in the view.py file called initializingdb()

jiangblog/presdebate/view.py after editing

Working with User Input Major Steps: 1.Adding an HTML form to our template. 2.Editing the application URLconf. 3.Adding a new view function that processes user input.

Adding an HTML Form to Our Template.

Cross-Site Request Forgery Django comes with a data-preserving feature that disallows POSTs which are not secure against cross-site request forgery (CSRF) attacks. You can read more about CSRF at the following website: srf/

Cross-Site Request Forgery For our simple application, two fixes: 1. Add a CSFR token ({% csrf_token %} to forms that POST back to your site 2. Send the request context instance to the token via the template.

Editing the Application URLconf.

Adding a New View Function that Processes User Input

Trying Out the Django Admin Django provides a automatic back-end administration application, or Admin for short. Why do we need the Admin? 1. Testing during the development stage, even before the UI has been developed. 2. Manage the site easily.

Steps to Set up the Admin for a Django Project 1. Update the settings.py. Note that the Admin is also an application. 2. Run syncdb. 3. Editing the project URLconf file. 4. Register the PresDebate model with Admin.

Update the settings.py

Run syncdb

Editing the Project URLconf File.

Register the PresDebate Model with the Admin We need to inform Django which models should show up for editing in the Admin screens. To do this, we will create a file named as admin.py in the presdebate directory.

Admin.py

This Time the Admin Worked!

We can also Edit/Add Comments Using the Admin

Editing Admin.py to Fix the “Display Issue”

Using Python Shell with Django Application 1. Run./manage.py shell This will bring up Ipython, which can be downloaded at: 2. Run./manage.py shell --plain This will bring up python.