Giter Site home page Giter Site logo

django-101's Introduction

Django 101

An introduction to the web framework Django.

Web App Types

The following graph shows a simplified model of the two web app ideal types.

How web apps work

How Django Works (Thin Client)

The following graph shows a simplified model of the inner workings of Django with focus on server-side-rendering (via templating) and some JavaScript (JS) usage on the client side.

How Django works

Why Django?

  • Batteries included (e.g. admin backend)
  • Fast development time
  • Many third-party packages
  • Django's Object Relational Mapping (ORM) is easy

Say Hello!

Let's build an app.

[This tutorial was made on an GNU/Linux operating system (Ubuntu 18.04) and might be adjusted for usage with other operating systems]

  • First you need Git, Python (>= 3.6) with Virtualenv and Pip installed. If you are under Linux/Mac this should be a no-brainer, if you use Windows and you don't have a Git, Python, Virtualenv and Pip workflow yet, please read this tutorial for Pip and Virtualenv installation/usage and visit this link to download/install Git.
  • If not already downloaded/installed, download/install Git, Python (>= 3.6), Pip (normally comes with Python) and Virtualenv
  • Open the console (aka terminal, shell) on your computer
  • Create a project directory in your terminal:
mkdir -p path/to/projectfolder
  • CD into your project directory:
cd path/to/projectfolder
  • Create a virtual environment with Virtualenv:
virtualenv venv --python=python3.6
  • Activate the virtual environment:
source venv/bin/activate
  • Install Django:
pip install django
  • Initialize Django project:
django-admin startproject hello_project
  • CD into your "Django" project directory:
cd hello_project
  • Initialize Django app:
python manage.py startapp hello_app
  • Add hello_app to installed apps in hello_project/settings.py:
INSTALLED_APPS = [
    ...
    'hello_app',
]
  • Create a model Person in hello_app/models.py:
from django.db import models


class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    def __str__(self):
        return '{} {}'.format(self.first_name, self.last_name)

  • Register Person model in admin panel in hello_app/admin.py:
from django.contrib import admin
from .models import Person

admin.site.register(Person)

  • Make migrations and migrate project:
python manage.py makemigrations
(Info: python manage.py showmigrations)
(Info: python manage.py sqlmigrate hello_app 0001)
python manage.py migrate
  • Create an admin account:
python manage.py createsuperuser
  • Start the development server:
python manage.py runserver
  • Go to 127.0.0.1:8000/admin and add some persons.
  • Create a view named hello in hello_app/views.py:
from django.shortcuts import render
from .models import Person


def hello(request):
    persons = Person.objects.all()
    context = {
        'persons': persons
    }
    return render(request, 'hello_app/hello.html', context)

  • Create an template folder in hello_app/templates/hello_app:
mkdir -p hello_app/templates/hello_app
  • Create an HTML template in hello_app/templates/hello_app/hello.html:
<!DOCTYPE html>
<html>
<head>
    <title>Hello App</title>
</head>
<body>
    <h1>Hello:</h1>
    {% for person in persons %}
    <li>{{ person.first_name }} {{ person.last_name }}</li>
    {% endfor %}
</body>
</html>
  • Register your hello view to an URL in hello_project/urls.py:
...
from hello_app.views import hello

urlpatterns = [
    ...
    path('', hello),
]
  • Go to 127.0.0.1:8000 and enjoy your very basic and not so nice looking app ๐Ÿ˜‰

Knowledge

django-101's People

Contributors

4lm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.