BLC Project PDF
BLC Project PDF
BLC Project PDF
Greg Loyse
1 Introduction 3
1.1 The Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Http and the Request / Response cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 The Client Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Take Away . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Setup 9
2.1 Project folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Installing Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Creating Django project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 settings.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 Creating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 Inspecting the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7 Running the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.8 Creating & installing the Blog App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Resources 19
i
ii
Introduction to web development with Python and Django Documentation, Release 0.1
Contents:
Contents 1
Introduction to web development with Python and Django Documentation, Release 0.1
2 Contents
CHAPTER 1
Introduction
There are a few things we need to explain before getting stuck in.
We focus on the overall picture. To do this we use a few analogies not to be taken too literally.
The Internet
The internet is a network of computers. Its goal is to enable communication between them.
A network is composed of nodes and edges. Visually it is a set of dots and connections. The London tube map is an
example.
Your family, friends, colleagues, and acquaintances can be thought of as a network of people. (This is how social
networks model our relationships.)
To communicate we must have a means by which our messages reach the intended destination.
On the one hand we need something physical to connect the computers. These are the wires.
On the other hand we need some conventions (software) to ensure messages reach their destinations.
One way this is done over the internet is called TCP/IP.
TCP ensures the messages arrive safely with nothing missing. Every computer has an IP which is a unique address.
You can think of TCP as an envelope and IP as the address on it.
To communicate effectively the elements of a network need to agree on some protocol. That protocol for humans can
be english but there are other ‘protocols’, chinese for example.
Many computers on the internet use Http to communicate.
3
Introduction to web development with Python and Django Documentation, Release 0.1
Every time you click on a link, or type a url and enter into a browser, you are making what is called an http GET
request.
Here is an example that uses curl from the command line as a client:
< </div>
< </body>
< </html>
4 Chapter 1. Introduction
Introduction to web development with Python and Django Documentation, Release 0.1
In software development an architecture is a way of organising code you see time and time again. Its also called a
pattern. Similar perhaps to how journalists follow a pattern when structuring their articles.
Think about the meaning of the words.
A browser is a great example of a client. It sends http requests to a server. A server returns an http response, which
the browser then renders as a web page.
We will see other examples of a client - server architecture when we introduce using databases.
HTML
<!doctype html>
<html>
<head>
<title>Example Domain</title>
</head>
<body>
<div>
<h1>A Header</h1>
<p>Here is some text between p elements</p>
</div>
</body>
</html>
Databases
Exercise
6 Chapter 1. Introduction
Introduction to web development with Python and Django Documentation, Release 0.1
Note we have same information we found with curl above. It is presented in a more user friendly way however.
Explore one of your favourite websites using the developer tools to inspect what is going on at the http network level.
Take Away
All internet experiences, online shopping, news, videos, sending texts... boil down to computers sending messages
much like what we have described above.
Http is not the only protocol in town, but the concept of computers acting as clients and servers communicating by
sending requests and responses is almost universal.
8 Chapter 1. Introduction
CHAPTER 2
Setup
Project folder
mkdir website
cd website
Installing Django
$ pip freeze
To install a Django:
9
Introduction to web development with Python and Django Documentation, Release 0.1
website
- manage.py
- website
- __init__.py
- settings.py
- urls.py
- wsgi.py
settings.py
You will see some output such as: Creating table auth_user
You just installed Django's auth system, which means you don't have any superusers
˓→defined.
10 Chapter 2. Setup
Introduction to web development with Python and Django Documentation, Release 0.1
Now the top level folder website contains a file called db.sqlite3. This is your database.
sqlite3 db.sqlite3
The sqlite3 program provides a new type of shell which is meant for inspecting our database.
Here is an example interaction:
sqlite>
The .tables command lists all the tables that exist in the database. We recognise these as being the same that were
created earlier by running the .manage.py syncdb command.
The select * from auth_user; is SQL. SQL is a language dedicated to programming databases. This command means
give me everything in the auth_user table.
Type:
sqlite3> .quit
To exit.
./manage.py runserver
Now you can send http requests using your browser as client. Enter:
http://127.0.0.:8000/
You can quit the server at any point by pressing together cntrl + c
Tip:
Django like any framwork, provides a way of organising your code. It provides in effect a proven archi-
tecture which you learn to work within.
A good webframework makes a lot of decisions for you. You build on the combined experience of the
developpers who created it.
Django introduces the concept of an app as a way to organise code.
Our Blog will be an app. We create it thusly:
- blog
| - __init__.py
| - admin.py
| - models.py
| - tests.py
| - views.py
- db.sqlite3
- manage.py
- website
- __init__.py
- settings.py
- urls.py
- wsgi.py
We now need to tell our website about the blog apps’ existence. We do this by adding it to the INSTALLED_APPS
tuple.
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
12 Chapter 2. Setup
Introduction to web development with Python and Django Documentation, Release 0.1
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
'blog',
)
14 Chapter 2. Setup
CHAPTER 3
We will start by programming the server to return a responses to an http GET request.
We will always need to do two things:
• map a url to a view function
• define the view function
website/urls.py
Saying hello
def hello(request):
return HttpResponse('hello')
Now we need to configure our website with which request will trigger this view function. We do this by adding a line
to website/urls.py:
15
Introduction to web development with Python and Django Documentation, Release 0.1
urlpatterns = patterns('',
url(r'^hello$', 'blog.views.hello'),
url(r'^admin/', include(admin.site.urls)),
)
GET parameters
http://localhost:8000/whoami/?name=greg&sex=male
The parameter section is defined by ? followed by & separated keys and values.
Here we have the parameters: - name, equal to greg - sex, equal to male
As usual we need to do two things create a view function and hook it up in website/urls.py
First the view function:
def whoami(request):
sex = request.GET['sex']
name = request.GET['name']
response = 'You are ' + name + ' and of sex ' + sex
return HttpResponse(response)
Note that we can extract anything passed in the url after the ? character using the request.GET dictionary.
Now website/urls.py:
urlpatterns = patterns('',
url(r'^$', 'blog.views.hello'),
url(r'^time$', 'blog.views.time'),
url(r'^whoami/$', 'blog.views.whoami'),
url(r'^admin/', include(admin.site.urls)),
)
You should now get as a response: You are greg and of sex male
Exercises
A clock service
Program your server to response the time when it recieves an http GET request to this url:
http://localhost:8000/time
You will need to create a view function in blog/views.py, and hook it up to a url in website/urls.py.
You have just been contracted by the NHS to provide a service that calculates the BMI. Both other websites and mobile
apps will be using your service.
The endpoint (url) will respond successfully to the following type of url:
bmi/?mass=75&height=182
Look up the BMI equation on wikipedia, and write a bmi view function and hook it up to the website urls.
You may have to revisit the notion of type in Python. Remember there is a difference between ‘5’ and 5.
To transform a number as a string into a number you can cast it using either int() or float():
>>> float('5')
5.0
>>> int('5')
5
Your Serivce
By now you have discovered that you can trigger any type of programming sending ba GET request to your server.
You simply hook up a url to a view function.
Come up with something that is useful to you!
Anything that involves simple maths is easily explored.
Solutions:
You can find some suggestions by adding _solutions to the above url.
3.4. Exercises 17
Introduction to web development with Python and Django Documentation, Release 0.1
Resources
19