Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
Django is a Web server framework under Python. There are many different frameworks under Python. Django is one of the most iconic players in the heavyweight class. Many successful websites and apps are based on Django. Although Django is not in Python, it is not rail to Ruby's unified, but Django is undoubtedly a leader in Python's Web application.
Chanquintin's tribute, "The liberated Jiang GE"
Django accidentally Quintin the same name as the movie. This greatly increases the visibility of Django. In addition, the use of the film, propaganda the pronunciation of this strange word.
Here is the first experience of Jiang GE.
Installing Django
Start Python on your computer and try to load the Django module. If it can be loaded successfully, then Django is already installed:
Import Djangoprint (Django. VERSION)
If Django is not yet installed, you can try using PIP installation at the command line:
sudo pip install Django
or use Easy_install:
sudo easy_install Django
In the night, Jiang GE and shackles say goodbye.
Start
Use the following command to create the project:
django-admin.py Startproject MySite
In the current directory, the MySite folder is generated. Its file tree structure is as follows:
Mysite├──manage.py└──mysite ├──__init__.py ├──settings.py ├──urls.py └──wsgi.py1 directory, 5 Files
Enter MySite and start the server:
Python manage.py Runserver 8000
The 8000 above is the port number. If not stated, the port number defaults to 8000.
Open the browser, Access http://127.0.0.1:8000, you can see the server is already running:
Although there is a server that can run, but what content is not.
Bounty Hunter? Jiang GE full face puzzled.
First page
As you can see in the HTTP protocol, the network server is the "request-response" mode of operation. The client sends a request to the URL, and the server starts the cook after the request, and finally dishes the guest. The MVC structure that Django uses, namely order, kitchen, storeroom separates.
We need a commander to assign the URL to an object processing, which requires a urls.py setting under Mysite/mysite. Python assigns URL requests to a chef based on the program.
Mysite├──manage.py└──mysite ├──__init__.py ├──settings.py ├──urls.py └──wsgi.py1 directory, 5 Files
Modify the urls.py to:
From Django.conf.urls import patterns, include, Urlfrom django.contrib import adminadmin.autodiscover () Urlpatterns = Patterns (', # Examples: # URL (r ' ^$ ', ' mysite.views.home ', name= ' home '), # URL (r ' ^blog/', include (' Blog.urls '), url (r ' ^admin/', include (Admin.site.urls)), URL (r ' ^$ ', ' mysite.views.first_page '),)
We added the last line. It assigns the URL of the root directory to an object for processing, and this object is mysite.views.first_page.
The object used to process the HTTP request does not yet exist, and we create the views.py under Mysite/mysite and define the First_page function in it:
#-*-Coding:utf-8-*-from django.http import httpresponsedef first_page (Request): return HttpResponse ("<p> World Good </p> ")
The first line describes the character encoding as Utf-8, which prepares for the use of Chinese below. The function of the First_page function is to return an HTTP reply, that is, <p> world good </p> here. First_page has a parameter request that contains the specific information requested, such as the type of the request, which is not used here.
The page works as follows:
Jiang Ge took the gun and shot it out. "My goodness!" The Germans were stunned.
Add app
A Web site may have multiple features. Instead of throwing everything into a folder, we can manage it in Django, app-based, modular. Under MySite, run manange.py to create a new app:
$python manage.py Startapp West
This new app is called West and is used for Western food.
In our root directory, there is a new folder called West.
Mysite/├──manage.py├──mysite│├──__init__.py│├──__init__.pyc│├──settings.py│├──settings.pyc│├──urls.p Y│├──views.py│└──wsgi.py└──west ├──admin.py ├──__init__.py ├──models.py ├──tests.py └──v iews.py
We also need to modify the project settings to indicate that we want to use West. In mysite/setting.py, in Installed_apps, add "West":
Installed_apps = ( ' django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contrib.sessions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' West ' ‘,)
You can see that in addition to the newly added West,django, some functional apps have been loaded by default, such as user authentication, session management, displaying static files, and so on. We will explain their use in the future.
Jiang Ge saw the former foreman, the eyes full of anger.
Add App Page
We add the homepage for the app below. We previously set the URL Access object in mysite/urls.py. is still set in a similar way.
On the other hand, in order to decouple and implement modularity, we should set the URL Access object in west/urls.py. Specific as follows:
First, modify the mysite/urls.py:
From Django.conf.urls import patterns, include, Urlfrom django.contrib import adminadmin.autodiscover () Urlpatterns = Patterns (', # Examples: # URL (r ' ^$ ', ' mysite.views.home ', name= ' home '), # URL (r ' ^blog/', include (' Blog.urls '), url ( R ' ^admin/', include (Admin.site.urls)), url ( R ' ^$ ', ' mysite.views.first_page '), url ( R ' ^west/', include (' West.urls ')),)
Note the newly added last line. Here, we remind the Commander, for West/'s visit, to refer to west/urls.py.
Then we create the west/urls.py and add the content:
From Django.conf.urls import patterns, include, Urlurlpatterns = Patterns ("', url (r ' ^$ ', ' west.views.first_page '), )
The URL corresponds to the First_page function in west, views.py.
Finally, under West, modify the views.py to:
#-*-Coding:utf-8-*-from django.http import httpresponsedef first_page (Request): return HttpResponse ("<p> Western </p> ")
Visit Http://127.0.0.1:8000/west to see the effect.
"You bastards, I'll never let go!" "Jiang ge Roar.
Summarize
As you can see, the Django creation process is very simple. But this is just the first attempt at Django. To create a full-featured website, you also need to invoke many of the other Django features.
The liberation of Jiang GE has only just begun.
Liberated Jiang Ge 01 to the horizon