Intro To Django
Intro To Django
Displays info
to user and Application
accepts logic, data,
interactions. and rules.
Source: https://www.tech101.in/streamline-your-system-with-the-mvc-model/
MVC in Web Apps
Views - web pages or code that generates web pages.
Views may be passive (HTML) or interact with user via
code such as Javascript in web pages.
settings. urls.py
py
HTTPResponse app/views.py
(controller)
render
From http://django-easy-tutorial.blogspot.com/2017/03/django-request-lifecycle.html
MVC or MVT?
Django says the framework uses "Model-View-Template"
not MVC design pattern.
https://www.youtube.com/watch?v=GGkFg52Ot5o
(5 minutes)
Structure of a Django Project
Create a project named "mysite".
cmd> django-admin startproject mysite
Creates:
mysite/
manage.py
script to start, stop, test, or
update the project
mysite/
subdirectory for
__init__.py
settings.py project settings and
urls.py configuration
wsgi.py
"mysite" configuration directory
Every Django project has a project configuration dir.
# apps.py
class Polls(AppConfig):
name = 'polls'
models.py
Define Model classes containing data and application
logic. Model objects are saved to a database.
This is one of the most important parts of your app!
# models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(
'question', max_length=100)
pub_date = models.DateTimeField(
'date published')
def isPublished(self):
return datetime.now() > self.pub_date
migrations/
A directory containing "SQL migrations".
When you change the structure of models, the structure of
the database tables (schema) must be updated to match.
Django creates an "SQL migration" in this directory
whenever you run:
python manage.py makemigrations
migrations/
0001_initial.py
0002_add_closing_date.py
tests.py
A file for unit tests of your app.
Putting all your tests in one file is not a good idea.
We will later replace this file with a tests/ directory.
# tests.py
from django.test import TestCase
class QuestionModelTest(TestCase):
def test_create_question(self):
q = Question(question_text="...")
q.save()
self.assertTrue(q.id > 0)
...
Use django.test.TestCase
Use django.test.TestCase instead
of Python's TestCase.
Django's TestCase adds important
features.
- creates a "test" database in-
memory before each test.
- extra assert methods, like
assertInHTML, assertRedirects
- provides a Client class for testing
views.
views.py
A file for your "view" methods that handle requests from
the user. views may also be classes.
Views often provide data for an HTML "template" and tell
Django to render it, as in example below.
# views.py
def index(request):
"""show index of recent poll questions"""
questions = Question.objects.order_by('id')[:10]
return render(request, 'polls/index.html',
{'question_list':questions})
...
views, requests, and responses
Django creates an HttpRequest object from the data in the
HTTP request received from the web.
It gives this request object to the view.
A view returns an HttpResponse that Django returns.
def index(request):
"""show index of recent poll questions"""
questions = Question.objects.order_by('id')[:10]
return render(request, 'polls/index.html',
{'question_list':questions})
render() creates HttpResponse object
...
Templates
Web apps return customized HTML pages.
Apps inject data values into a "template" for an HTML
page. A "rendering engine" processes the template.
Templates may include other templates.
render( HTML Page
list_view.py template, including
list = ... list) Rendering data from list
template = Engine
'List_template'
process