Django Step 1 (zz)

Source: Internet
Author: User

The first step is to grasp three parts of a Web framework.

1. Prepare the development environment

2. Create a project and run

3. Develop a hello World Application

 

1. Prepare the environment

First, install Python and Django. This official website has a detailed description, and there are also many tutorials on the Internet. I will not repeat them here, just to express my views on the operating system:

Mac OS:ProgramFriendly staff and users
Linux: very friendly to programmers
Widows: user friendly
Which operating system is used.

Then select the development tool. We recommend that you discard the IDE and use a good text editor. Vim is strongly recommended. But if you choose Emacs, I have nothing to say.

 

2. Create a project

As a Web framework, Django should first be able to see the page in the browser. If the environment has been installed.

First create a project: django-admin.py startproject depot, that is, start creating a project named depot.

Compared with rails, the process is quiet and the results are clean. As follows:

Depot/
_ Init _. py
Manage. py
Settings. py
URLs. py
The functions of these files are as follows:

_ Init _. py: Python module definition file. This is an empty file. You do not need to modify it.
Manage. py: a command line tool that generates this file for convenience. You can use Python manage. py help to view the functions of the tool. You do not need to edit this file.

Settings. py: settings or configurations of this Django project.
URLs. py: Set the URL of the Django project.
Unlike rails, Django has very few initial project files and can easily read allCode. However, these files constitute a runable Django application.

Enter the project directory and run the project:

CD depot/
Python manage. py runserver

 

You can see some prompts:

Validating models...

0 errors found
Django version 1.3, using settings 'depot. setting'
Development server is running at http: // 127.0.0.1: 8000/
Quit the server with CONTROL-C.
[29/Jan/2012 02:09:17] "Get/HTTP/1.1" 200 2049

In this case, the web server (Development Environment !) You have run it. visit http: // 127.0.0.1: 8000/in a browser and you can see the following interface:

It indicates that Django has started to work.

 

3. Hello Django!

Unlike rails, Django does not need to generate a lot of files such as controller, helper, and view. To implement a hello program, just a few lines of code are required.
Django Web applications usually contain four parts: urlconf, view, template, and model (see urlconf + MTV: MVC in Django eyes). However, these parts are not completely required. For example, we want to implement the simplest "Hello, Django! ", You only need to define urlconf and view.

Let's clarify the "requirement". Hello, Django! The following functions are implemented: Enter http: // 127.0.0.1: 8000/hello in the browser, and "hello Django!" is displayed !".

First, you must implement a view to respond to the request. In Django, The View Graph is a function that accepts an httprequest parameter and returns an httpresponse. We can define this function anywhere, but it is usually stored in the Views. py file of the Django app. In hello and Django, you do not need to create a Django app (because you do not need a model). Therefore, you can create a view in the project directory. and define the hello (request) view function:

Depot/views. py:

 

FromDjango. HTTPImportHttpresponse

DefHello (request ):
ReturnHttpresponse ("Hello, Django!")

 

Next, map the previously defined URL to this view function. This is done by urlconf. The essence of urlconf is the ing between the URL mode and the view functions to be called for this URL mode.
Open the generated URLs. py file, first import the view just created in front of the file, and then add the hello ing to the tuple type variable urlpatterns:

 

FromDjango. conf. URLs. defaultsImportPatterns, include, URL
FromDepot. ViewsImportHello

Urlpatterns = patterns ('',
URL (R'^ Hello/$', Hello ),
)

Access http:/127.0.0.1: 8000/Hello. Hello, Django!

 

Urlconf is easy to understand, that is, each item in urlpatterns is a binary group (Regular Expression, view function ). When Django receives an HTTP request, it finds the matching expression from urlpatterns, sends the request to the corresponding view function, and finally returns an HTTP Response to Django for processing. That's all.

So far, Django's first step has been taken. At least you can start CGI-style Web development.

 

Reposted from: Django Step 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.