Django Basics

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Introduction

What is django?

: Python web framework

  • High-level Python web framework that enables rapid development of secure and maintainable websites

  • Free & open source

web framework

  • Aims to reduce difficulties encountered in web page development process

  • A collection of classes and libraries that implement the standard structure of applications

Why do we have to learn django?

  1. Portable

    • Python based which means it runs on many platforms

  2. Complete

    • Django follows the "Batteries included" philosophy

  3. Versatile

    • Can be used to build almost any type of website

      • Can work with any client-side framework

      • Can deliver content in almost any format (including HTML, RSS feeds, JSON, XML, etc)

  4. Secure

    • Provides a secure way to manage user accounts & passwords

      • By avoiding common mistakes like putting session information in cookies where it is vulnerable

        • Instead cookies just contain a key

        • And the actual data is stored in the db

      • By avoiding directly store passwords rather than password hash

  5. Scalable

    • Uses a component-based "shared-nothing" architecture

      • each part of the architecture is independent of the others

      • can be replaced or changed if needed

    • Can scale for increase traffic by adding hardware at any level

      • caching servers

      • database servers

      • application servers

  6. Maintainable

    • Follows DRY (Don't Repeat Yourself) principle

      • No unnecessary duplication

    • Promotes the grouping of related functionality into reusable

  7. Many companies are utilizing it

    • Spotify

    • Instagram

    • Dropbox

    • Delivery Hero

    • etc.

Web protocol we know

  • Request

  • Response

How does django work?

  • Django follows the MVC (Model View Controller) pattern.

    • MVC: One of the Software design patterns

      MVC Pattern
      django

      Model

      Model

      View

      Template

      Controller

      View

  • MTV

    • M: Data management

    • T: Interface (screen)

    • V: Intermediate management (interaction)

img

Run django on CS 50 IDE

CS 50 IDE

: CS50 IDE is a cloud-based Integrated Development Environment powered by AWS Cloud9arrow-up-right that features a cloud-based Ubuntu environment, a browser-based editor that supports syntax highlighting and word completion, a GUI-based GDB debugging, themes, customizable layouts, keyboard shortcuts, and many more features. Since it's cloud-based, you can continue working on your problem sets even if you use a different computer!

AWS Cloud 9

Cloud-based IDE (Integrated Development Environment)

  • OS: Ubuntu 18.04.4 LTS

    • python 3.7.6

  • Advantage of not affecting the local development environment

image-20200326100727731

Django installation

  • Download version 2.1.15

Django uninstall

Create project

Run server

  • Edit line 28 of settings.py

  • Always check the directory where the command is executed when running the server

  • For the running server, click the URL in the right area

    image-20200326102907761
  • To stop the server, use ctrl + c in terminal

Server running screen

image-20200326102049790

Create app

Start Project

  • project is the atomic unit of Django!

Created project folder structure

image-20200326122807719

Create Pages

Git

INSTALLED_APPS

: Registration

image-20200326131656480
  • Added pages for registration

Start Server

image-20200326124046226

Check Port Number

  • Can confirm connection to ports 80 and 8080

Check IP

Django has DEBUG=True as default

urls.py

Gatekeeper

  • path('request URL/', handling view(controller) function)

    • Django's characteristic is that URLs end with /!

pages/views.py

  • Every time hello() is called, the object that called the function enters as the first argument

    • When defining a function in view, always define the first argument as request!

  • First argument - request

  • Second argument - file name

  • Third argument - context

    • Context must always be a dictionary!

Create pages/templates folder

  • The name must be templates

Kill running python files

Wrap-up

App Creation

  • Django consists of one project with multiple apps

    • ex) Creating a community

      • App related to members - accounts

      • App related to posts - posts

  • After creating an app, must register it in INSTALLED_APPS in settings.py

Basic Flow

1. URL Configuration

  • urls.py

    • URLs in path are always closed with /

2. View Configuration

  • views.py

  • When defining functions, always write the first argument as request

    • Why? Because internally when processing requests, it needs to send an object containing request information when calling functions

  • Return through render function

    • First argument: request

    • Second argument: template file (html)

    • Third argument

      • dictionary type

      • Pass values to be used in template

3. Create templates file

  • The html file to return is always created inside the templates folder

Last updated