MODULE 1- FSD-Notes
MODULE 1- FSD-Notes
BACHELOR OF ENGINEERING
DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING
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
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".
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
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
Prepared By,
Ranjitha J
Assistant Professor
Dept, of ISE
SJB Institute of Technology