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

MODULE 1- FSD-Notes

The document provides an overview of the Django web framework, including its MVC design pattern, evolution, and key concepts such as views, URL mapping, and error handling. It highlights Django's focus on reusability and its origins from real-world web development needs. Additionally, it discusses the importance of loose coupling in Django's architecture and offers insights into creating dynamic web applications using Django's features.

Uploaded by

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

MODULE 1- FSD-Notes

The document provides an overview of the Django web framework, including its MVC design pattern, evolution, and key concepts such as views, URL mapping, and error handling. It highlights Django's focus on reusability and its origins from real-world web development needs. Additionally, it discusses the importance of loose coupling in Django's architecture and offers insights into creating dynamic web applications using Django's features.

Uploaded by

Ranjitha J
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SJB Institute of Technology, Bangalore – 60

BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

Name of the Course: FSD (18CS745)


NOTES – Module-1

Chapter 1
What is a Web Framework
MVC Design Pattern
Django Evolution
Chapter 3
Views
Mapping URL to Views
Working of Django URL Confs and Loose Coupling
Errors in Django
Wild Card patterns in URLS
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

Chapter 1
What is a Web Framework
Django is a Python framework that makes it easier to create web sites using Python. Django takes care of
the difficult stuff so that you can concentrate on building your web applications.
Django emphasizes reusability of components, also referred to as DRY (Don't Repeat Yourself), and comes
with ready-to-use features like login system, database connection and CRUD operations (Create Read
Update Delete).
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

MVC Design Pattern


CGI code using Django. The first thing to note is that we split it over three Python files (models.py,
views.py, urls.py) and an HTML template (latest_books.html):
The main thing to note here is the separation of concerns:
• The models.py file contains a description of the database table, represented by Python class. This
class is called a model. Using it, you can create, retrieve, update, and delete records in your database
using simple Python code rather than writing repetitive SQL statements.
• The views.py file contains the business logic for the page. The latest_books() function is called a
view.
• The urls.py file specifies which view is called for a given URL pattern. In this case, the URL /latest/
will be handled by the latest_books() function. In other words, if your domain is example.com, any
visit to the URL http://example.com/latest/ will call the latest_books() function.
• The latest_books.html file is an HTML template that describes the design of the page. It uses a
template language with basic logic statements—for example, {% for book in book_list %}.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

Django Evolution
It’s useful to understand why Django was created, because knowledge of the history will put into context
why Django works the way it does. If you’ve been building Web applications for a while, you’re probably
familiar with the problems in the CGI example we presented earlier. The classic Web developer’s path
goes something like this:
1. Write a Web application from scratch.
2. Write another Web application from scratch.
3. Realize the application from step 1 shares much in common with the application from
step 2.
4. Refactor the code so that application 1 shares code with application 2.
5. Repeat steps 2–4 several times.
6. Realize you’ve invented a framework.
Django grew organically from real-world applications written by a Web-development team in Lawrence,
Kansas, USA. It was born in the fall of 2003, when the Web programmers at the Lawrence Journal-World
newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications.
The World Online team, responsible for the production and maintenance of several local news sites,
thrived in a development environment dictated by journalism deadlines. For the sites—including
LJWorld.com, Lawrence.com, and KUsports.com—journalists (and management) demanded that features
be added and entire applications be built on an intensely fast schedule, often with only days’ or hours’
notice. Thus, Simon and Adrian developed a timesaving Web-development framework out of necessity—
it was the only way they could build maintainable applications under the extreme deadlines.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

In summer 2005, after having developed this framework to a point where it was efficiently powering most
of World Online’s sites, the team, which now included Jacob Kaplan-Moss, decided to release the
framework as open-source software. They released it in July 2005 and named it Django, after the jazz
guitarist Django Reinhardt.
Now, several years later, Django is a well-established open-source project with tens of thousands of users
and contributors spread across the planet. Two of the original World Online developers (the “Benevolent
Dictators for Life,” Adrian and Jacob) still provide central guidance for the framework’s growth, but it’s
much more of a collaborative team effort. This history is relevant because it helps explain two key things.
The first is Django’s “sweet spot.” Because Django was born in a news environment, it offers several
features (such as its admin site) that are particularly well suited for “content” sites—sites like Amazon.com,
Craigslist, and The Washington Post that offer dynamic, database-driven information. Don’t let that turn
you off, though—although Django is particularly good for developing those sorts of sites, that doesn’t
preclude it from being an effective tool for building any sort of dynamic Web site. (There’s a difference
between being particularly effective at something and being ineffective at other things).
The second matter to note is how Django’s origins have shaped the culture of its open-source community.
Because Django was extracted from real-world code rather than being an academic exercise or a commercial
product, it is acutely focused on solving Web-development problems that Django’s developers themselves
have faced—and continue to face. As a result, Django itself is actively improved on an almost daily basis.

Chapter 3
Views
The first view –
As our first goal, let's create a web page that outputs that famous example message: Hello World.
Create a mysite directory, within that directory create an empty file called views.py.

•First, you import the class HttpResponse, which lives in the django.http module. You need to import this
class because it’s used later in the code.
•Next, you define a function called hello—the view function.
•Each view function takes at least one parameter, called request by convention. This is an object that
contains information about the current Web request that has triggered this view, and it’s an instance of the
class django.http.HttpRequest. In this example, you don’t do anything with request, but it must be the first
parameter of the view nonetheless.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

•Note that the name of the view function doesn’t matter; it doesn’t have to be named in a certain way for
Django to recognize it. We called it hello because that name clearly indicates the gist of the view, but it
could just as well be named hello_wonderful_ beautiful_world, or something equally revolting. The next
section, “Your First URLconf,” will shed light on how Django finds this function.
•The function is a simple one-liner: it merely returns an HttpResponse object that has been instantiated
with the text "Hello world".

Mapping URL to Views


The Second view – As for dynamic content, let's create a web page that outputs example : Date_time.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

To add a URL and view to the URLconf, just add a Python tuple mapping a URL pattern to the view
function. Here’s how to hook in the hello view

Two changes were made:


•First, the hello view was imported from its module: mysite/views.py, which translates into
mysite.views in Python import syntax. (This assumes that mysite/views.py is on the Python path; see the
sidebar for details.)
•Next, the line ('^hello/$', hello) was added to urlpatterns. This line is referred to as a URLpattern.
It’s a Python tuple in which the first element is a pattern-matching string (a regular expression; more on
this in a bit) and the second element is the view function to use for that pattern.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

The third view: Dynamic URLs

Working of Django URL Confs and Loose Coupling


The loose coupling is a software-development approach that values the importance of making pieces
interchangeable. If two pieces of code are loosely coupled, then changes made to one of the pieces will have
little or no effect on the other. Django's URLconfs are a good example of this principle in practice. In a
Django web application, the URL definitions and the view functions they call are loosely coupled; that is,
the decision of what the URL should be for a given function, and the implementation of the function itself,
reside in two separate places.
For example, consider our current_datetime view. If we wanted to change the URL for the application-say,
to move it from /time/ to /current-time/-we could make a quick change to the URLconf, without having to
worry about the view itself. Similarly, if we wanted to change the view...
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

One important detail introduced here is that r character in front of the regular expres sion string. This
character tells Python that the string is a “raw string”—its contents should not interpret backslashes. In
normal Python strings, backslashes are used for escaping special characters—such as in the string '\n',
which is a one-character string containing a newline. When you add the r to make it a raw string, Python
does not apply its backslash escaping, so r'\n' is a two-character string containing a literal backslash and a
lowercase n. There’s a natural collision between Python’s use of backslashes and the backslashes that are
found in regular expressions, so it’s strongly suggested that you use raw strings any time you’re defining a
regular expression in Python. From now on, all the URLpatterns in this book will be raw strings.
Now that a wildcard is designated for the URL, you need a way of passing that wildcard data to the view
function, so that you can use a single view function for any arbitrary hour offset. You can do this by
placing parentheses around the data in the URLpattern that you want to save. In the case of the example,
you want to save whatever number was entered in the URL, so put parentheses around \d{1,2}, like this:

Errors in Django
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

The datetime.timedelta function expects the hours parameter to be an integer, and the bit of code that
converted offset to an integer was commented out. That caused datetime.timedelta to raise the TypeError.
It’s the typical kind of small bug that every programmer runs into at some point.
The point of this example was to demonstrate Django error pages.
The error page and get to know the various bits of information it gives you. Here are some things to notice:
• At the top of the page, you get the key information about the exception: the type of exception, any
parameters to the exception (the "unsupported type" message in this case), the file in which the exception
was raised, and the offending line number.
• Under the key exception information, the page displays the full Python traceback for this exception.
This is similar to the standard traceback you get in Python’s command line interpreter, except it’s more
interactive. For each level (“frame”) in the stack, Django displays the name of the file, the
function/method name, the line number, and the source code of that line.
• Click the line of source code (in dark gray), and you’ll see several lines from before and after the
erroneous line, to give you context.
• Click “Local vars” under any frame in the stack to view a table of all local variables and their
values, in that frame, at the exact point in the code at which the exception was raised. This debugging
information can be a great help.
• Note the “Switch to copy-and-paste view” text under the “Traceback” header. Click those words,
and the traceback will switch to an alternate version that can be easily copied and pasted. Use this when
you want to share your exception traceback with others to get technical support—such as the kind folks in
the Django IRC chat room or on the Django users’ mailing list.
• Underneath, the “Share this traceback on a public Web site” button will do this work for you in just
one click. Click it to post the traceback to http://www.dpaste.com/, where you’ll get a distinct URL that
you can share with other people.
• Next, the “Request information” section includes a wealth of information about the incoming Web
request that spawned the error: GET and POST information, cookie values, and metainformation, such as
Common Gateway Interface (CGI) headers.
• Below the “Request information” section, the “Settings” section lists all the settings for this
particular Django installation.
SJB Institute of Technology, Bangalore – 60
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

Wild Card patterns in URLS

Prepared By,
Ranjitha J
Assistant Professor
Dept, of ISE
SJB Institute of Technology

You might also like