0% found this document useful (0 votes)
6 views

Django2 DjangoFundamentals2

Uploaded by

Alaa Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Django2 DjangoFundamentals2

Uploaded by

Alaa Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MVT

• MVT is a software design pattern for developing


web applications.
• It has the following three parts:
• Model: The interface of the data. It defines
the logical data structure and the it is the
handler between the database and the View
• View: encapsulate the logic responsible for
processing a user’s request and for returning
the response.
• Template: The template layer provides a
designer-friendly syntax for rendering the
information to be presented to the user.

17
Writing Views

• Inside the views.py module of our app we can


define how to handle an HTTP request and what
response to send.
• Django has three ways to write a view:
• Function-based views (FBV): a function that receives
an HTTP request and return a Response. (request
handler)

• Class-based views (CBV)


Simple Function-based view
• Generic class-based views (GCBV)
18
Mapping URLs to Views 1

• Inside the app folder create a new file called urls.py


• Import the path() function: from django.urls import path
• Import the views module from current app folder
• define urlpatterns: list of paths or URLPattern objects
• Add to it path(‘EntryPoint’, views.(your_view_function))

19
Mapping URLs to Views 2

• Import the app URLConf into the main project URLConf


• Import the include() function: from django.urls import path, include
• Add a URL to urlpatterns: path(‘AppEntryPoint', include(‘app_urls_file'))

• A URLconf is similar to a table of


contents for Django-powered web site.
• It's a mapping between URL patterns
and the view functions that need to be
called for those URLs.

20
Mapping URLs to Views 3

21
Using Templates

• Templates are used to return an HTML


content to the client
• Inside the app folder create a new folder
templates.
• Inside the templates folder create a new
folder with the name of the app
• Inside this inner folder create an html file
(any name)
• Modify the view function to render the
created html file

22
Passing Data to
Templates

• When rendering the views add a third argument


to render function, context.
• Context is a dictionary of key-value pairs
• render(request, template_name,
{‘parm1’:’value1’, ‘param2’:’value2’…})
• Access the passed parameters inside template
using {{ }}.

23
• Middleware is a framework of hooks into
Django’s request/response processing.
• It’s a light, low-level “plugin” system for

Adding Django Debug Toolbar •


globally altering Django’s input or output.
Each middleware component is
responsible for doing some specific
function

Go to https://django-debug-toolbar.readthedocs.io/en/latest/installation.html and follow instructions as follow


1. Open a new terminal in VSCode

2. pipenv install django-debug-toolbar

3. add ‘debug_toolbar’ in the list of INSTALLED_APPS in settings of the project


4. add the debug toolbar to the URLCon of the project as follows:
1. open urls.py inside the project folder
2. import debug_toolbar
3. add another URL inside urlpatterns:
5. add a middleware for the debug_toolbar at the top inside the list of MIDDLEWARE in the project settings file.

6. add the IP of your website in the list of INTERNAL_IPS, you should add the whole settings to the settings file of your project. In
development the IP address is local and it is the localhost address:

24
25

You might also like