Installation pip install django
environment variable : C: \ Program Files \ Anaconda3 \ Scripts
django-ADMIN startproject mysite to create a django project
mysite directory
-mysite # configure the entire program
- __init__
-settings # profile
-urls #URL correspondence between
-wsgi # rule interface is used to help create a socket set django follow wsgi formal specification using nginx + is uwsgi
-manage.py # django management program
- python manage.py run django
- python manage.py startapp create App
- python manage.py makemigrations ORM framework automatic generation of database operations database
- python manage.py migrate orm framework automatically generates database operations database
python.exe manage.py runserver 127.0.0.1:8000 start django web page
supportwsgi接口的模块:
server_names = {
'cgi': CGIServer,
'flup': FlupFCGIServer,
'wsgiref': WSGIRefServer,
'waitress': WaitressServer,
'cherrypy': CherryPyServer,
'paste': PasteServer,
'fapws3': FapwsServer,
'tornado': TornadoServer,
'gae': AppEngineServer,
'twisted': TwistedServer,
'diesel': DieselServer,
'meinheld': MeinheldServer,
'gunicorn': GunicornServer,
'eventlet': EventletServer,
'gevent': GeventServer,
'geventSocketIO':GeventSocketIOServer,
'rocket': RocketServer,
'bjoern' : BjoernServer,
'auto': AutoServer,
}
ORM: relation object mapping
chouti
-chouti
- Configuration
- Master App
- Admin App
# create App
Python manage.py startapp CMDB
Python manage.py startapp OpenStack
Python manage.py startapp moniter
App:
Record migrations operations that modify the database table structure
Admin admin django offered
to build the table will complain reference https://blog.csdn.net/jiangxunzhi123/article/details/86160146
Apps configure the current App
Models ORM, write to the specified class, you can create a database structure through the command
tests the unit test
views service code
1, the path configuration template
tEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'the DIRS': [the os.path.join (base_dir, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
2,配置静态目录:
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
)
<link rel="stylesheet" href="/static/commons.css"/>
<script src="/static/jquery-1.12.4.js "> </ Script> the Submit submit a post
url submission is GET
settings in
middlerware
# comment CSRF
GET: Gets data
POST: submit data
to define routing rules :
url.py
"the Login" ---> function name
is defined view function
views.py in the App
DEF FUNC (Request):
# request.method GET / the POST
#http: //127.0.0.1:? 8000 / Home NID = 123 & Alex name =
# request.GET.get ( '', None) # acquisition request sent from the data
# request.POST.get ( '', None )
#return the HttpResponse ( "string")
#return the render (Request, "the path Html template")
#return the redirect ( '/ fill only url')
templates to render
special template language
- {{variable name}}
DEF FUNC (Request):
return the render (Request, 'index.html', { 'current_user':"alex"})
index.html
<html>
<body>
<div>{{current_user}}</div>
</body>
</html>
===>最后生成的字符串
<html>
<body>
<div><alex></div>
</body>
</html>
---->For循环
def func(request):
return render(request,'index.html',{'current_user':"alex",'user_list':['lei','leo']})
index.html
<html>
<body>
<div>{{current_user}}</div>
<ul>
{% for row in user_list%}
{% if row=="alex"%}
<li>{{row}}</li>
{%endif%}
{%endfor%}
</ul>
</body>
</html>
索引
def func(request):
return render(request,'index.html',{'current_user':"alex",
'user_list':['lei','leo'],
'user_dict':{'k1':'v1','k2':'v2'}})
index.html
<html>
<body>
<div>{{current_user}}</div>
<a>{{user_list.0}}</a>
<a>{{user_dict.k1}}</a>
<a>{{user_dict.k2}}</a>
</body>
</html>
条件
def func(request):
return render(request,'index.html',{'current_user':"alex",
'age':18,
'user_list':['lei','leo'],
'user_dict':{'k1':'v1','k2':'v2'}})
index.html
<html>
<body>
<div>{{current_user}}</div>
<a>{{user_list.0}}</a>
<a>{{user_dict.k1}}</a>
<a>{{user_dict.k2}}</a>
{% if age %}
<a>有年龄</a>
{% if age >%}. 19 <a> small meat </a> {%}% the else
<a> old </a>
% endif%} {
{} the else
<a> to false </a>
{% endif%}
</ body>
</ HTML>
Django request lifecycle
user ---> the URL correspondence relation (match) -> view function - -> returned to the user (string)
user ---> the URL correspondence relation (match) - open an HTML file>, read the contents -> view function
operation :
XXOO management:
MySQL
SQLAlchemy
host management table :
Ip
port
service line
...
user table :
username
password
feature :
1, log
2, host management page
- view all host information (4)
- increase host information (8) ** modal dialog box
3, see more
url:
"detail" -->detail
def detail(request):
nid=request.GET.get("nid")
v=select * from tb where id=nid;
...
4,删除
del_host ->delete _host
def delete_host(request):
nid=request.POST.get('nid')
delete from tb where id=nid
return redirect('/home')
django supplementary notes
Guess you like
Origin www.cnblogs.com/leiwenbin627/p/10981013.html
Recommended
Ranking