Montréal-Django
https://slides.com/canweb/django-101/live/
http://invite.yuldev.ca/
ctrlweb-performa-guest / joyeuxnoel
CEO @ ctrlweb
Board-member @ CANWEB
@danidou
dleblanc78
daniel@canweb.org
Business Logic
Data Access
Presentation
User
url
View
Model
Template
"MVT" Design Architecture
Model - View - Template
a cousin of "MVC"
Model - View - Controller
User
url
View
Model
Template
Uniform Resource Locator
Defines the points of access to your app.
Requests are routed according to it.
User
url
View
Model
Template
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'),
]User
url
View
Model
Template
The "Business Logic Layer".
A bridge between Model and Template.
In "MVC", this would be the Controller.
User
url
View
Model
Template
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)User
url
View
Model
Template
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
User
url
View
Model
Template
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)User
url
View
Model
Template
The "Presentation Layer".
Defines how data is represented.
Recipe to build web documents.
User
url
View
Model
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 %}http://docs.python-guide.org/en/latest/dev/virtualenvs/
Install Homebrew (https://brew.sh/)
/usr/bin/ruby -e \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"Install Python 3
brew install python3Install Pipenv
pip3 install --user pipenvInstall Python3
sudo apt install python3 python3-pip python3-dev rake ruby-devUpdate pip
pip3 install --upgrade pipInstall Pipenv
pip3 install --user pipenvCreate a project directory (i.e. ~/projects/django-101)
cd ~/projects
mkdir django-101Install Django within a Virtual Environment
cd ~/projects/django-101
pipenv install DjangoActivate the Virtual Environment and initialize your project
pipenv shell
django-admin startproject django101
cd django101
./manage.py migrate
./manage.py runserverhttps://docs.djangoproject.com/en/2.0/intro/tutorial01/
Suggestions?