FSD 1
FSD 1
Module 1 Syllabus
Web framework, MVC Design Pattern, Django Evolution, Views, Mapping URL to Views,
Working of Django URL Confs and Loose Coupling, Errors in Django, Wild Card patterns in
URLS.
Web framework
Web framework provides implementation for common tasks like database connection
setup and teardown, security, admin interface, authentication packages etc. so that
programmer need not rewrite the code from scratch.
Streamlined development
Speed and Efficiency
Integrated Security
Community Support
Enhanced performance & scalability
Testing and Debugging
MVC defines a way of developing software so that the code for defining and accessing
data (the model) is separate from request routing logic (the controller), which in turn is
separate from the user interface (the view). A key advantage of MVC approach is that
components are loosely coupled.
Django Evolution
Django was introduced in the year 2003, when the Web programmers at the
Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began
using Python to build applications.
In July 2005 It was released as open source software.
September 2008: Version 1.0
July 2009: Version 1.1
May 2010: Version 1.2
March 2011: Version 1.3
March 2012: Version 1.4
Feb 2013: Version 1.5
November 2013: Version 1.6
September 2014: Version 1.7
April 2015: Version 1.8
Dec 2015: Version 1.9
Aug 2016: Version 1.10
April 2017: Version 1.11
Dec 2017: Version 2.0
Aug 2018: Version 2.1
April 2019: Version 2.2
Dec 2019: Version 3.0
Installation Steps
3. Update pip in the virtual environment by running the following command prompt:
python -m pip install --upgrade pip
4. Install Django in the virtual environment by running the following command in the
VS Code Terminal:
python -m pip install Django
Django creates the directory structure as shown in above figure. Observe the files
and folders.
12. Change directory to webproject1 and create Django app use the following
command (A project can have many apps)
python manage.py startapp app1
urlpatterns = [
path('admin/', admin.site.urls),
path('app1/',include('app1.urls')),
]
Create urls.py file in app1 folder and include the following content
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello, name="hello"),
]
matches, it calls the view function associated with that pattern, passing an
HttpRequest object as the first parameter to the function.
The view function is responsible for returning an HttpResponse object.
When an HTTP request comes in from the browser, a server-specific handler constructs
the HttpRequest passed to later components and handles the flow of the response
processing. The handler then calls any available Request or View middleware.
Middleware:
Once the request reaches Django, it passes through the middleware stack. Middleware
can intercept requests and responses, perform additional processing, and modify the
request or response objects. Common uses of middleware include authentication, session
management, CSRF (Cross-Site Request Forgery) protection, and content compression.
If a view function raises an exception, control passes to the exception middleware. If this
middleware does not return an HttpResponse, the exception is reraised. Django includes
default views that create a friendly 404 and 500 response. Finally, response middleware
is good for postprocessing an HttpResponse just before it’s sent to the browser or doing
cleanup of request-specific resources.
Regular Expressions
Regular expressions (or regexes) are a compact way of specifying patterns in text. While
Django URLconfs allow arbitrary regexes for powerful URL-matching capability.
Symbol Matches
. Any character
\d Any digit
[^/]+ All characters until a forward slash (excluding the slash itself)
Develop a Django app that displays current date and time in server and date and time
with <offset> hours ahead and <offset> hours before as an offset of current date and
time in server.
# views.py
from datetime import datetime, timedelta
from django.http import HttpResponse
def current_datetime(request):
return HttpResponse(datetime.now())
def hours_ahead(request,offset):
current_datetime = datetime.now()
datetime_ahead = current_datetime + timedelta(hours=int(offset))
datetime_before = current_datetime - timedelta(hours=int(offset))
display = "<h1>Date and Time Offset</h1>"
display += "<p>Current Date and Time: "+str(current_datetime)+"</p>"
display +="<p>Date and Time "+offset+" Hours Ahead: "+ str(datetime_ahead)
+"</p>"
display +="<p>Date and Time "+offset+" Hours Before: "+str(datetime_before)
+"</p>"
return HttpResponse(display)
# urls.py in dateapp
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^time/$',views.current_datetime,name="current_datetime" ),
re_path(r'^time/plus/(?P<offset>\d{1,2})/$',views.hours_ahead,name="hours_ahead" ),
]
# urls.py in dateproj
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('date/', include('dateapp.urls')),
]
# settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dateapp',
]
Errors
Django displays the message Page Not Found (404) because the requested URL is not
defined in URLconf. Django also displays precisely which URLconf Django used and every
pattern in that URLconf. From that information, we should be able to tell why the
requested URL threw a 404.