Intro to Web Development Using Python and Django

Post on 21-Jan-2018

197 Views

Category:

Education

4 Downloads

Preview:

Click to see full reader

Transcript

Intro to Web Development Using Python and Django

October 2017

Chariza Pladinchariza.b.pladin@accenture.com

Chariza Baclor Pladin

2

- Bachelor of Science in Information

Technology (2014)

- I.T Instructor

- Data Analyst / Mobile App QA

Accenture Inc.

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

App● Creating and Populating

Database● URLs and Views● Q/A

Course Outline

3

Seminar Schedule

1 Hour Discussion

4

3 Hours Code Labs

● 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

Introduction to Python

6

Python is your friend.

7

Hello, Django

8

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

/Why choose Django?/

10

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

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

Why use Django?

13

Setting up and Basic Requirements

14

Requirements and Downloads

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

15

Requirements and Downloads

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

16

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

Virtual Environment (cont.)

Create Virtual Environment

Command: virtualenv -p python3 env

Virtual environment name

18

Virtual Environment (cont.)

Activate Virtual Environment

Command:source env/bin/activate

19

Modify Pycharm Interpreter

Change Interpreter settings

● Preferences ○ Project First Project

■ Project Interpreter ● Name of virtual environment

20

Install Django

Command:pip install django

21

Create our firstDjango Web App

22

Create Web App

Command: django-admin startproject <project_name>

Example: django-admin startproject mysite

23

Create Web App(cont.)

24

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.

Create Web App(cont.)

26

Main Website

App 1 App 2 App nth

Create Polls App

27

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.

Polls App(cont.)

29

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.

First run Server

31

Command:python manage.py runserver

First run Server(cont.)

32

Adding Models

33

Models

34

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

Models(cont.)

35

Models - Polls

36

Question Choice

Number of VotesChoice TextPublish DateQuestion

Text

Link

Creating the Models

37

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’.

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.)

Make Migrations

40

Make Migrations(cont.)

41

Populating Database

42

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

Populating Database - Add a Question

44

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

45

Populating Database - Add Choices

46

Django Admin Tool

47

Admin Tool

48

Command: python manage.py createsuperuserPython manage.py runserver

Important NoteThis command will prompt asking for

username, email address and a password.

Admin Tool(cont.)

49

Admin Tool(cont.)

50

Adding Database files to Admin Tool

51

Open admin.py

Adding Database files to Admin Tool(cont.)

52

Adding Database files to Admin Tool(cont.)

53

Adding Database files to Admin Tool(cont.)

54

Adding Database files to Admin Tool(cont.)

55

Playing with URLs

56

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.

Create New URL

58

Create New URL

59

Open mysite directory > urls.py

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

60

Configure new urls.py

61

Open polls directory > urls.py

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.

Configure views.py

63

Open polls directory > views.py

Then run the server againCommand:

python manage.py runserver

Configure views.py

64

Setting up views

65

Setting up views

66

Open polls directory > views.py

Linking views to URLs

67

Open polls directory > urls.py

Display Questions from database

68

Open polls directory > views.py

Creating Templates

69

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.

71Creating Templates

72Creating Templates(cont.)

73Creating Templates(cont.)

Django Templates(cont.)

74

Important Note:

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

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

Django Templates(cont.)

75

Step 1: Create the template

Django Templates(cont.)

76

Step 1: Rendering templates

Quick Links

77

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

Quick Links(cont.)

78

https://www.edx.org/

Quick Links(cont.)

79

Sharing is caring...

80

FREE PDFS!

81

Thank you :)

82

Send me feedback :)

top related