intro to web development using python and django

82
Intro to Web Development Using Python and Django October 2017 Chariza Pladin [email protected]

Upload: chariza-pladin

Post on 21-Jan-2018

198 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Intro to Web Development Using Python and Django

Intro to Web Development Using Python and Django

October 2017

Chariza [email protected]

Page 2: Intro to Web Development Using Python and Django

Chariza Baclor Pladin

2

- Bachelor of Science in Information

Technology (2014)

- I.T Instructor

- Data Analyst / Mobile App QA

Accenture Inc.

Page 3: Intro to Web Development Using Python and Django

● Introduction to Python● Hello, Django● Setting up Our First Django

App● Creating and Populating

Database● URLs and Views● Q/A

Course Outline

3

Page 4: Intro to Web Development Using Python and Django

Seminar Schedule

1 Hour Discussion

4

3 Hours Code Labs

Page 5: Intro to Web Development Using Python and Django

● Understand agenda’s outline

● Know and be familiarized

with some basic concept

about Python programming

language

● Write simple Python

program and

Django-powered web

page/sites

Outcomes

5

Page 6: Intro to Web Development Using Python and Django

Introduction to Python

6

Page 7: Intro to Web Development Using Python and Django

Python is your friend.

7

Page 8: Intro to Web Development Using Python and Django

Hello, Django

8

Page 9: Intro to Web Development Using Python and Django

DJANGO

● A high-level Python web Framework● Encourages rapid development, clean and pragmatic

design● ‘For perfectionists with deadlines’ ● ‘Focuses and automation and DRY’● Widely supported and has many deployment options.

9

Page 10: Intro to Web Development Using Python and Django

/Why choose Django?/

10

Page 11: Intro to Web Development Using Python and Django

Why use Django?

● The framework has templates, libraries and API designed to work together for natural growth and connectivity.

● Django suits projects of any size, from small to the biggest ones.

● Django uses Python which was one of the most popular programming languages of 2015, and is now the most popular language for those learning to code.

11

Page 12: Intro to Web Development Using Python and Django

Why use Django?

● Django is a more fully featured kit than most of other frameworks, it contains everything you need to build an app.

● Django adheres to D.R.Y. — Don’t Repeat Yourself — philosophy. That means that the framework places a premium on getting the absolute most out of very little code.

12

Page 13: Intro to Web Development Using Python and Django

Why use Django?

13

Page 14: Intro to Web Development Using Python and Django

Setting up and Basic Requirements

14

Page 15: Intro to Web Development Using Python and Django

Requirements and Downloads

https://www.python.org/downloads/

15

Page 16: Intro to Web Development Using Python and Django

Requirements and Downloads

https://www.jetbrains.com/pycharm/

16

Page 17: Intro to Web Development Using Python and Django

Virtual Environment

- An isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects It enables multiple side-by-side installations of Python, one for each project.

Install Virtual EnvironmentCommand:

pip3 install virtualenv17

Page 18: Intro to Web Development Using Python and Django

Virtual Environment (cont.)

Create Virtual Environment

Command: virtualenv -p python3 env

Virtual environment name

18

Page 19: Intro to Web Development Using Python and Django

Virtual Environment (cont.)

Activate Virtual Environment

Command:source env/bin/activate

19

Page 20: Intro to Web Development Using Python and Django

Modify Pycharm Interpreter

Change Interpreter settings

● Preferences ○ Project First Project

■ Project Interpreter ● Name of virtual environment

20

Page 21: Intro to Web Development Using Python and Django

Install Django

Command:pip install django

21

Page 22: Intro to Web Development Using Python and Django

Create our firstDjango Web App

22

Page 23: Intro to Web Development Using Python and Django

Create Web App

Command: django-admin startproject <project_name>

Example: django-admin startproject mysite

23

Page 24: Intro to Web Development Using Python and Django

Create Web App(cont.)

24

Page 25: Intro to Web Development Using Python and Django

Mysite - Components

25

● manage.py - lets app creator talk through terminal/shell.

● __init__.py - tells file is a python package.

● settings.py - holds the setting configuration of all the app

inside the web app.

● urls.py - has the access and settings of any url used.

● wsgi.py - used to deploy web app to a server.

Page 26: Intro to Web Development Using Python and Django

Create Web App(cont.)

26

Main Website

App 1 App 2 App nth

Page 27: Intro to Web Development Using Python and Django

Create Polls App

27

Page 28: Intro to Web Development Using Python and Django

Create Polls App

28

Command:python manage.py startapp polls

Important NoteAlways make sure to check active directory

(before executing code) which is the <project_name> directory.

Page 29: Intro to Web Development Using Python and Django

Polls App(cont.)

29

Page 30: Intro to Web Development Using Python and Django

Polls Directory - Components

30

● admin.py - -allows user to add features in the admin page

● __init__.py - tells file is a python package.

● apps.py - used to configure apps.

● models.py - database layout.

● tests.py - used to app testing.

● views.py - used to display database content.

Page 31: Intro to Web Development Using Python and Django

First run Server

31

Command:python manage.py runserver

Page 32: Intro to Web Development Using Python and Django

First run Server(cont.)

32

Page 33: Intro to Web Development Using Python and Django

Adding Models

33

Page 34: Intro to Web Development Using Python and Django

Models

34

Object-oriented programming (OOP) is a style of programming that focuses on using objects to design and build applications.

Page 35: Intro to Web Development Using Python and Django

Models(cont.)

35

Page 36: Intro to Web Development Using Python and Django

Models - Polls

36

Question Choice

Number of VotesChoice TextPublish DateQuestion

Text

Link

Page 37: Intro to Web Development Using Python and Django

Creating the Models

37

Page 38: Intro to Web Development Using Python and Django

Make Migrations

38

Command: python manage.py makemigrations pollsPython manage.py migrate

Important NoteGo to settings.py under the <project_name>

and don’t forget to add ‘polls’.

Page 39: Intro to Web Development Using Python and Django

Make Migrations(cont.)

39

What’s the use of Migration?

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.)

Page 40: Intro to Web Development Using Python and Django

Make Migrations

40

Page 41: Intro to Web Development Using Python and Django

Make Migrations(cont.)

41

Page 42: Intro to Web Development Using Python and Django

Populating Database

42

Page 43: Intro to Web Development Using Python and Django

Open Python Shell

43

The Python interactive console (also called the Python interpreter or Python shell) provides programmers with a quick way to execute commands and try out or test code without creating a file.

Command:python manage.py shell

Page 44: Intro to Web Development Using Python and Django

Populating Database - Add a Question

44

Page 45: Intro to Web Development Using Python and Django

Populating Database(cont.) - Display Question and Choice Objects

45

Page 46: Intro to Web Development Using Python and Django

Populating Database - Add Choices

46

Page 47: Intro to Web Development Using Python and Django

Django Admin Tool

47

Page 48: Intro to Web Development Using Python and Django

Admin Tool

48

Command: python manage.py createsuperuserPython manage.py runserver

Important NoteThis command will prompt asking for

username, email address and a password.

Page 49: Intro to Web Development Using Python and Django

Admin Tool(cont.)

49

Page 50: Intro to Web Development Using Python and Django

Admin Tool(cont.)

50

Page 51: Intro to Web Development Using Python and Django

Adding Database files to Admin Tool

51

Open admin.py

Page 52: Intro to Web Development Using Python and Django

Adding Database files to Admin Tool(cont.)

52

Page 53: Intro to Web Development Using Python and Django

Adding Database files to Admin Tool(cont.)

53

Page 54: Intro to Web Development Using Python and Django

Adding Database files to Admin Tool(cont.)

54

Page 55: Intro to Web Development Using Python and Django

Adding Database files to Admin Tool(cont.)

55

Page 56: Intro to Web Development Using Python and Django

Playing with URLs

56

Page 57: Intro to Web Development Using Python and Django

URLs and Views

57

2 step process to display data:

1. Link urls.py in the main directory to the new urls.py in the poll directory.

2. Link views.py to the file.

Page 58: Intro to Web Development Using Python and Django

Create New URL

58

Page 59: Intro to Web Development Using Python and Django

Create New URL

59

Open mysite directory > urls.py

Page 60: Intro to Web Development Using Python and Django

Step 2: Create a new urls.py inside polls directory

60

Page 61: Intro to Web Development Using Python and Django

Configure new urls.py

61

Open polls directory > urls.py

Page 62: Intro to Web Development Using Python and Django

HTTP Response

62

Django uses request and response objects to pass state through the system.

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.

Page 63: Intro to Web Development Using Python and Django

Configure views.py

63

Open polls directory > views.py

Then run the server againCommand:

python manage.py runserver

Page 64: Intro to Web Development Using Python and Django

Configure views.py

64

Page 65: Intro to Web Development Using Python and Django

Setting up views

65

Page 66: Intro to Web Development Using Python and Django

Setting up views

66

Open polls directory > views.py

Page 67: Intro to Web Development Using Python and Django

Linking views to URLs

67

Open polls directory > urls.py

Page 68: Intro to Web Development Using Python and Django

Display Questions from database

68

Open polls directory > views.py

Page 69: Intro to Web Development Using Python and Django

Creating Templates

69

Page 70: Intro to Web Development Using Python and Django

Django Templates

70

Django’s template engine provides a powerful mini-language for defining the user-facing layer of your application, encouraging a clean separation of application and presentation logic.

Page 71: Intro to Web Development Using Python and Django

71Creating Templates

Page 72: Intro to Web Development Using Python and Django

72Creating Templates(cont.)

Page 73: Intro to Web Development Using Python and Django

73Creating Templates(cont.)

Page 74: Intro to Web Development Using Python and Django

Django Templates(cont.)

74

Important Note:

Django Template Language use different coding syntax for loops and variables.

{% %} - used for loops.{{ }} - used for variables

Page 75: Intro to Web Development Using Python and Django

Django Templates(cont.)

75

Step 1: Create the template

Page 76: Intro to Web Development Using Python and Django

Django Templates(cont.)

76

Step 1: Rendering templates

Page 77: Intro to Web Development Using Python and Django

Quick Links

77

● https://www.djangoproject.com/● https://github.com/django/django● https://djangobook.com/● https://www.fullstackpython.com/django.html● https://djangopackages.org/

Page 78: Intro to Web Development Using Python and Django

Quick Links(cont.)

78

https://www.edx.org/

Page 79: Intro to Web Development Using Python and Django

Quick Links(cont.)

79

Page 80: Intro to Web Development Using Python and Django

Sharing is caring...

80

Page 81: Intro to Web Development Using Python and Django

FREE PDFS!

81

Page 82: Intro to Web Development Using Python and Django

Thank you :)

82

Send me feedback :)