Django 101

Montréal-Django

https://slides.com/canweb/django-101/live/
http://invite.yuldev.ca/
ctrlweb-performa-guest / joyeuxnoel

Welcome

Daniel LeBlanc

CEO @ ctrlweb

Board-member @ CANWEB

@danidou

dleblanc78

daniel@canweb.org

Who already has working knowledge of Python?

Who's confortable with the command line?

Basic Concepts

Separation of concerns

Business Logic

Data Access

Presentation

Basic Concepts

User

url

View

Model

Template

"MVT" Design Architecture

Model - View - Template

 

a cousin of "MVC"

Model - View - Controller

Basic Concepts

User

url

View

Model

Template

url

Uniform Resource Locator

Defines the points of access to your app.

Requests are routed according to it.

Basic Concepts

User

url

View

Model

Template

url

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

Basic Concepts

User

url

View

Model

Template

View

The "Business Logic Layer".

A bridge between Model and Template.

In "MVC", this would be the Controller.

Basic Concepts

User

url

View

Model

Template

View

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

Basic Concepts

User

url

View

Model

Template

Model

The "Data Access Layer".

Defines everything about data :

- How to access it

- How to validate it

- Which behaviour it has

- Its relations with other data 

Basic Concepts

User

url

View

Model

Template

Model

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Basic Concepts

User

url

View

Model

Template

Template

The "Presentation Layer".

Defines how data is represented.

Recipe to build web documents.

Basic Concepts

User

url

View

Model

Template

Template

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

Make sure your environment is ready

http://docs.python-guide.org/en/latest/dev/virtualenvs/

Step 1

Install Homebrew (https://brew.sh/)

Step 1 - Mac OS X

/usr/bin/ruby -e \
    "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Python 3

brew install python3

Install Pipenv

pip3 install --user pipenv

Install Python3

Step 1 - Ubuntu 16.04

sudo apt install python3 python3-pip python3-dev rake ruby-dev

Update pip

pip3 install --upgrade pip

Install Pipenv

pip3 install --user pipenv
  • PyCharm Community Edition
  • Atom
  • Sublime Text 3
  • Eclipse
  • Vim
  • ...

Step 1 - IDE?

It really is up to you!

Starting your project

Step 2

Create a project directory (i.e. ~/projects/django-101)

Step 2

cd ~/projects
mkdir django-101

Install Django within a Virtual Environment

cd ~/projects/django-101
pipenv install Django

Activate the Virtual Environment and initialize your project

pipenv shell
django-admin startproject django101
cd django101
./manage.py migrate
./manage.py runserver

Creating your first app

Step 3

Django's official tutorial 

Choice #1

https://docs.djangoproject.com/en/2.0/intro/tutorial01/

We decide on a project together, and we wing it! 

Choice #2

Suggestions?

Made with Slides.com