1. Create a Django Project
Django-admin Startproject Project Name
2. Create an App
CD project name Python manage.py Startapp CMDB
3. Static file Configuration
Edit project.settings.py, append the following:
Staticfiles_dirs = (Os.path.join (base_dir, "Static"),)
4. Template path Configuration
DIRS ==> (Os.path.join (base_dir, ' templates '),)
5, settings, note csrf
middleware_classes = (# ' Django.middleware.csrf.CsrfViewMiddleware ',)
6, define the routing rules, in url.py define the URL of user access by which function to process the request
url.py "Login"---Name of function
7, define the View function, define the function in views.py
def func (reuest): # request.method Get/post # http://127.0.0.1:8000/home?nid=123&name=bob # request. Get.get (", None) #获取请求发来的数据 # request. Post.get (", None) # return HttpResponse (" string ") # return Render (Request, ' HTML template path ', {" User_list ": User_list}) # RE Turn redirect (' Can only fill in URLs, for example:/home ')
8. Template rendering
Django's special template language, such as:
--Variable Name value--
Define the variables and corresponding values in views.py, and use {{Var_name}} in the template's HTML page to take the value
def func (Request): Return render (Request, "index.html", {' Current_User ': "Bob"}) Index.html
--for Loop Value--
Define lists and values in views.py, and use {{for loop}} in the template's HTML page to take the value
def func (Request): Return render (Request, "index.html", {' Current_User ': "Bob", ' user_list ': [' Bob ', ' aaa ', ' BBB ']}) I ndex.html
--Index value--
Define the dictionary and value in views.py, and use {{Var_name.key}} in the template's HTML page to take the value
def func (Request): Return render (Request, "index.html", {' Current_User ': "Bob", ' user_list ': [' Bob ', ' AAA ') , ' BBB '], ' user_dict ': [' K1 ': ' v1 ', ' K2 ': ' V2 ']}) index.html
--Condition value--
{% if%}
{% if%}
{% ENDIF%}
{% else%}
{% Else%}
{% ENDIF%}
Def func (Request): return render (Request, "index.html",{ ' Current_User ': "Bob", ' age ': 18, ' user_list ': [' Bob ', ' aaa ', ' BBB ', ' user_dict ': [' K1 ': ' v1 ', ' K2 ': ' v2 '] }) index.html
Workflow for templates:
1. When a user requests a request via a URL, the Django route sends the request to the view function,
2, then the view function will first go to the template,
3. Render the variable with {{Current_User}} in the template with the value defined in the view and convert the template content to a string after rendering
4. Return the generated string as a whole to the user
Filter filters
Template filters are the way that variables are converted before they are displayed, and look like this
{{Name|lower}}
This displays the value of the {{name}} variable filtered by the lower filter, which converts the text to lowercase
Use (|) Pipeline to apply for a filter
filters can be strung into chains, that is, the result of a filter can be passed to the next, The following is the customary usage of escape text content and then convert the newline to P tags
{{My_text|escape|linebreaks}}
For more information on Django templates, please visit:
http://blog.csdn.net/zhangxinrun/article/details/8095118/
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1931369
Django Development Web site steps