Web Development Chapter 9 - Django
Template: Function (MVT)
- Template provides the HTML files for the application - The Template contains the place to be update based on the user request
HTTP Message Categories
- Two Types: Request, Response - Written in ASCII
Django: Processing user request
1. Django maps the URL to the correct view 2. The mapping is done in the URL configuration file 3. Django does the URL mapping and calls the correct view code 4. Django handles the passing of the response back to the client
HTTP Request Message
1. Request line 2. Headers 3. A blank line 4. Body (present only in some messages)
HTTP Response Message
1. Status line 2. Headers 3. A blank line 4. Body (present only in some messages)
What are the most common commands that mange.py provides?
1. runserver 2. startapp 3. shell 4. dbshell 5. makemigrations
What command line command creates a django project?
>django-admin startproject "projectName"
manage.py
A command-line utility that lets you interact with this Django project in various ways. It is used to manage the Django project ex: mange.py runserver - starts the Django development server on the local computer
dir command
Command-line command that shows the files and folders in the current directory
Django Project Structure
Django Project Directory contains 1. manage.py file 2. myproject directory
What can Django be used for?
Django can be used to build almost any type of website. It offers complete framework solution that provides everything needed to quickly create and deploy web projects. Content management systems Social networks and news sites. Data analysis Machine learning E-commerce platforms
Django project servers
Django has a built in development server which is used to run applications instantly without any external web server - This means we dont need another server to run the application in development mode
URL Mapping
Django needs to now which view function should be executed when it receives request for a particular URL ➢ The URL mapping is to build a link between a URL and a view function ➢ The URL mapping is configured in the urls.py file
Where is the URL mapping configured?
In the urls.py file
MVT (Model View Template)
MVT (Model View Template) is a software design pattern composed of three components 1. Model 2. View 3. Template
Model: Function (MVT)
Model provide abstraction to SQL database through ORM
MVT: Model
Model: handles data storage into a database
MVT: View
Query the model to handle the display
MVT: Template
Render the data to the web browser
HTTP: Status Code
Status code appears in the first line in server-to-client response message
Django development server structure
The development server start at the following URL by default:http://127.0.0.1:8000/ • The development server will listen to port 8000 on the localhost IP address: 127.0.0.1 • The development server watches the Djando project directory and will restart automatically every time you save a file so that any code changes your make are automatically reloaded into the server • What you want to stop the run server command, it can be done in the usual way for stopping processes in the terminal by typing Ctrl + C
What files are created with the command: >django-admin startproject myproject?
The following files are created - _innit_.py - asgi.py - settings.py - urls.py - wsgi.py
HTTP: Hyper Text Transfer Protocol
The protocol used to access data on the web HTTP defines how - web clients request web pages from web servers - web servers transfer web pages to web clients
asgi.py and wsgi.py
These files are what ASGI or WSGI web servers use to communicate with your Django app when you deploy it to a production web server. You normally dont need to edit these at all, and they aren't used in day-to-day development
settings.py
This contains all the Django settings for your application
manage.py startapp
This creates a new Django app in your project
mange.py makemigrations
This generates database change instructions from your model definitions
urls.py
This has the global URL mappings that Django will initially use to locate views
_innit_.py
This is an empty file that lets Python know that the directory is a python module
manage.py runserver
This start the Django development HTTP server to serve the Django app on your local computer
manage.py shell
This starts a python interpreter with the Django settings pre-loaded. This is useful for interacting with your application without having to manually load your Django settings
manage.py dbshell
This starts an interactive shell connected to your database, using the default parameters from your Django settings. You can run manual SQL queries this way
What is the function of URL mapping?
To build a link between a URL and a view function
What are SQL statements used for?
To perform tasks such as update data, or retrieve data on a database Example: SELECT Customer_Name FROM Customers_Database;
How do you run a Django project?
Use the following command (inside project directory) >python manage.py runserver - this starts the Django development server
View: Function (MVT)
View provides the software logic of the application ➢ The web browser send the user request to the View ➢ The View responds back to the browser to satisfy the user request Example: ➢ View send a request to the database(HttpRequest) ➢ View render a template with the contents from the database(HttpResponse)
Web Framework
Web framework is a set of software components designed to simplify the web development process.
What does a Web Framework do?
Web framework provides a standard way to build and deploy web applications on the World Wide Web. - Web framework allows you to focus on the most important details and project's goals instead of creating things, that you can simply pull out of the framework.
Structured Query Language (SQL)
a database-specific language used in programming for managing data held in a relational database management system.
What is Django
a free and open-source framework that was designed for data-driven applications, where the front-end provides the user interface for a back-end database.
Object-Relation Mapper (ORM)
a programming technique that allow interaction with the database in the same way that you would with SQL.
cd command
changes directory cd myProject = changes directory to myProject
How do you create an application within a Django project?
execute the following command >python manage.py startapp myapp - Creates the app directory (myapp) inside the project directory (myproject)