Chapter I. Django INTRODUCTION 40
This book is about a Web development framework Django, which will save you a lot of time and make your web development fun. With Django, you can build a high-performance Web application with minimal expense.
6
On the bright side, Web development is exciting and creative, and on the flip side, it's a tedious and annoying job. By reducing repetitive code, Django allows you to focus on interesting and critical things in Web applications. To achieve this goal, Django provides a high level of abstraction of the common Web development model, provides a quick solution to frequent programming jobs, and provides a clear and unambiguous convention for "How to solve problems". At the same time, Django tries to leave some way for you to develop outside the framework as needed.
9
The purpose of this book is to develop you into a Django expert. The main focus is on two aspects: first, we'll explain in depth what Django does and how to build Web apps with her, and second, we'll discuss more advanced concepts where appropriate, and explain how to use them efficiently in your own projects. By reading this book, you will learn the skills to quickly develop powerful Web sites, and your code will be very clear and easy to maintain.
4
In this chapter, we'll take a glance at Django's full picture.
5 What is a Web framework? 3
Django is an excellent next-generation web framework, and what is the meaning of the term Django?
3
To answer this question, let's look at the development of a Web application by writing a standard CGI program, which is very popular in about 1998 years. When you write a CGI web app, you need to handle everything yourself, like you want to bake bread, but you need to make a fire yourself. Here is an example, a simple CGI script, written in Python, that reads the database and displays the latest published 10 books:
5
#!/usr/bin/pythonimport mysqldbprint "content-type:text/html" Printprint "
7The code is simple. First, output a line of Content-type according to the CGI requirements, followed by a blank line. Next is some HTML start tag, then connect to the database and perform some query operations to get the latest 10 books. Traverse these books and generate an unordered sequence of HTML. Finally, output the end tag of the HTML and close the database connection.
3Like this one-time dynamic page, writing from scratch is not the best way. At one point, the code is easy to understand, and even a novice developer can understand what the 16 lines of code do from the start to the end, without needing to read extra code. The same 16 lines of code are also easy to deploy: just save it in a file named "latestbooks.cgi", upload it to a Web server, and access it through a browser.
5But Web applications are far more complex, and this approach is no longer applicable, and you will face many problems:
What happens when multiple dynamic pages need to connect to the database at the same time? Of course, the code that connects the database should not exist in separate CGI scripts at the same time, so the most practical way is to reorganize the code into a common function.
1
What happens if a web designer has no Python experience at all, but needs to redesign the page? Ideally, the logic of the page display is separated from reading the book records from the database so that the redesign of the Web designer does not affect the previous business logic.
2
These are the issues that the web framework is dedicated to solving. The web framework provides a set of procedural frameworks for your application, so you can focus on writing clear, maintainable code without having to start from scratch. In simple terms, that's what Django can do.
MVC design Pattern
Let's look at a simple example where you can tell how the functionality of the web framework is different from the way it was before. Here's an example of how to do this by using Django:
# models.py (the database tables) from django.db import Modelsclass book (models. Model): name = models. Charfield (maxlength=50) pub_date = models. Datefield () # views.py (The business logic) from django.shortcuts import render_to_responsefrom models import Bookdef Lates T_books (Request): book_list = Book.objects.order_by ('-pub_date ') [: ten] return Render_to_response (' Latest_ Books.html ', {' book_list ': book_list}) # urls.py (the URL configuration) from Django.conf.urls.defaults import *import Viewsurlpatterns = Patterns (", (R ' latest/$ ', Views.latest_books),) # latest_books.html (the template) 8Do not worry about how this thing Works, we mainly want to let you know the overall design, here is the key to pay attention to the separation problem
2
models.py The file mainly uses a Python class to describe the data table. Called model . Using this class, you can create, retrieve, update, and delete records in a database with simple Python code without having to write one or two SQL statements.
These parts are loosely grouped together as model-view-controller (MVC) design patterns. Simply put, MVC is a method of software development that separates the definition of code and the method of data access (the model) from the request logic (Controller) and the user interface (view).
The key advantage of this design pattern is that the various components are loosely coupled . Thus, each Django-driven Web application has a clear purpose and can be changed independently without affecting other parts. For example, a developer changes a URL in an application without affecting the underlying implementation of the program. Designers can change the style of the HTML page without touching the Python code. The database administrator can rename the data table and simply change one place without needing to find and replace it from a large stack of files.
In this book, each component has its own chapter. For example, the third chapter covers the view, the fourth chapter is the template, and the fifth chapter is the model. The fifth chapter also discusses Django's MVC idea in depth.
2django history
Before we discuss the code, we need to look at Django's history first. Knowing some of the historical knowledge helps to understand why Django is building this framework because it helps to understand why Django works this way.
1
If you've ever written a Web application. Then you are likely to be familiar with our CGI example before. The development process for traditional Web developers is this:
1
Write a Web application from scratch.
Write another Web application from scratch.
Summarize from the first step (find the common code) and apply it in the second step.
Refactoring the code makes it possible to use the generic code in the 1th program in the 2nd program.
Repeat 2-4 steps several times.
Realize that you have invented a framework.
This is why Django was created!
Django grew from real-world applications and was written by a network development team in the city of Kansas (Kansas), Lawrence. It was born in the fall of 2003, when Programmers of the Lawrence Journal-world newspaper Adrian Holovaty and Simon Willison began using Python to write programs. At the time, their world Online team produced and maintained several local news sites and evolved in a fast-paced development environment that was unique to the press. These sites include ljworld.com, lawrence.com, and kusports.com, where journalists (or management) require additional features or the entire program to be built quickly within the planned time, typically only days or hours. As a result, Adrian and Simon developed a time-saving network program development framework that was the only way to complete the process before the deadline.
In the summer of 2005, when the framework was developed, it was used to make a lot of world Online sites. Jacob Kaplan-moss of the World Online Group decided to publish the framework as an open-source software. They were released in July in 2005 and named Django, from a famous jazz guitarist, Django Reinhardt.
While Django is now an open source project for developers worldwide, the original world Online developers still provide the main guidance to promote the growth of this framework. World Online has other important contributions, such as employee time, market materials, and the framework's host and bandwidth of Web sites (http://www.djangoproject.com/).
These histories are linked because they help explain the important two points. First, Django's cutest place, because Django was born in a news environment, she provides a lot of features (especially her management interface, see Chapter 6th), especially for content-providing websites such as ebay, Craigslist.org and washingtonpost.com provide a dynamic Web site based on the database. (not to be frustrated, although Django excels at dynamic content management systems, it doesn't mean that Django's primary purpose is to create dynamic content sites.) Some aspects are particularly efficient and other aspects of the non-efficiency are different)
Second, the origins of Django create her open source community, because Django comes from real-world code, not from a research project or a commercial product, and she focuses on solving the problems encountered in web development, as well as the problems that Django developers often encounter. In this way, Django advances on an ongoing basis every day. The framework's developers have a great interest in saving development time for developers, writing programs that are easier to maintain, while keeping programs running efficiently. Developers are self-motivated and try to save time and enjoy their work (to put it bluntly, they eat their own dog food.)
How to read this book 1
In writing this book, we try to make a balance between readability and reference, and of course this book is biased towards readability. The goal of this book, which has been mentioned before, is to develop you into a Django expert, and we believe that the best way to do this is through articles and ample examples, rather than providing a bunch of detailed but tedious manuals about Django features. (It was once said that if you just teach the alphabet, you can't teach people to talk.) )
According to this idea, we recommend reading 第1-7 chapters in order. These chapters form the basis for using Django, and after reading it, you can build a Web site that is supported by Django. The rest of the chapters focus on some of the other features of Django and can be read in any order.
The appendix is used as reference material. To recall the syntax or review a summary of the functionality of a part of Django, you may occasionally come back and turn over the materials and free documents on the http://www.djangoproject.com/.
Required Programming knowledge
Readers of this book need to understand basic process-oriented and object-oriented programming: Process Control ( if , while and for ), Data structures (lists, hash tables/dictionaries), variables, classes, and objects.
Web development experience, as you may think, is also very helpful, but it is not necessary to read this book. Through this book, we try to provide inexperienced developers with the best practices in web development.
Python knowledge required
In essence, Django is just a set of class libraries written in Python. Using Django to develop a site is to use these class libraries to write Python code. So the key to learning Django is learning how to program Python and understand how the Django class library works.
If you have a Python development experience, there should be no problem with the learning process, and basically, Django's code does not use some black magic (such as spoofing in code, which is difficult to interpret or understand). For you, learning Django is about learning about her naming conventions and APIs.
If you don't have the experience of using Python programming, you will certainly learn a lot of things. It is very easy to learn and easy to use. Although this book does not include a complete Python tutorial, it is a good introduction to Python features and features. Of course, we recommend that you read the official Python tutorial, which can be obtained online from http://docs.python.org/tut/. Also we recommend Mark Pilgrims's book Dive into Python (http://www.diveintopython.org/ )
New features of Django
As we mentioned earlier, Django improvements are frequent, and when this book is published, there may be some even very basic new features that will be introduced. Therefore, as the author, we have to reach two goals through this book:
Timely update the online version of this book, http://www.djangobook.com/ , so that after we complete the new chapter, you can get the latest and the best version.
If you want to use Django to implement features that are not mentioned in some books, please check the latest version of the book on the site mentioned above and remember to check the official Django documentation as well.
Get help
The biggest benefit of Django is that there is a group of helpful people in the Django community. You can ask all kinds of questions without restraint such as: from Django installation, app design, DB design, release.
-
-
The Django mailing list is where Django users ask questions and answer questions that can be
-
Http://www.djangoproject.com/r/django-users . Sign up for free.
Django IRC channel if the Django user encounters any tricky problems, expect a timely response to be able to use it. Join #django on freenode IRC Network
(This article turns from: http://djangobook.py3k.cn/chapter01/)
What is Django?