First, create the project:
Django-admin Startproject MySite
Second, to the directory, create the app:
# Learn is the name of an app
and add the app to the mysite/mysite/settings.py:
Installed_apps = ( ' django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contrib.sessions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' learn ' ,)
Define the function that handles the request:
Defined in file views.py:
# Coding:utf-8 from Import def Index (Request): return httpresponse (u" Welcome to MySite Project ")
Note the encoding.
Iv. correlation functions and requests:
Modify in mysite/mysite/urls.py:
fromDjango.conf.urlsImportinclude, url fromDjango.contribImportAdminurlpatterns=[url (r'^$','Learn.views.index', name='Home'), url (r'^admin/', include (Admin.site.urls)),#This line of system defaults]
Note: URLs are matched with regular expressions in the URL. The value of name is the function name that handles the URL, and string describes the path to the function.
v. Get the parameters in the request : (url= ' Http://localhost:8000/?a=123&b=bat ')
from django.shortcuts import render from django.http import Span style= "color: #000000;" > HttpResponse def add (request): #此为处理请求的函数 a = Request. Get[ " ' ' b = Request. Get[ " ' ] c = Int (a) + int (b) return HttpResponse (str (c))
Six, the use of the URL is flexible:
Eg: the website uses http://host:80/calc/3/4/this way
Change handler function: views.py
def add2 (request,a,b): = Int (a) + int (b) return HttpResponse (str (c))
More Correct the expression: urls.py
URL (r'^calc/(\d+)/(\d+)/$'calc.views.add2', name ='add2'),
vii. Front-end page template application:
Create a Templates folder under the app's directory and create a template file under that folder: Home.html (Django Project will look for template files in this directory by default)
[email protected] templates]$ lsad.html base.html bottom.html home.html nav.html tongji.html # these modules Version is referenced by the project. [email protected] templates]$ cat home.html{% extends'base.html'%}{% Block Title%}welcome home html{% endblock%} #覆盖默认内容, otherwise the default content inherited from Base.html is displayed. {% Block content%}{% include'ad.html'%} #引用ad. html template. this isIndex page,welcome! {% Endblock%}
Change the function that processes the request to return the template file to the client: (Of course, the content is rendered)
In the app/views.py
from Import def Home (Request): return'home.html' )
Viii. Start-up project:
[email protected] mysite]$Python manage.py runserver 0.0.0.0:8000Performing system checks ... System Check identified no issues (0silenced). October20, 2015-14:31:54Django version1.8.5, using Settings ' zqxt_tmpl.settings ' starting Development Server at http:0.0.0.0:8000/Quit the server with CONTROL-c.[20/oct/2015 14:31:54]"get/http/1.1"500 126680[20/oct/2015 14:32:07]"get/http/1.1"500 129469[20/oct/2015 14:33:37]"get/http/1.1"200 280
Python example-Start a Django project