1. jQuery
- 選擇
- 操做
- 事件
2. Http協議
-
3. Django框架
版本:1.11
建立:
django-admin startprojcet xxx
cd xxx
python manage.py startapp app01
python manage.py runserver 127.0.0.1:8000
今日內容:
1. 請求生命週期
2. 主機管理
- 路由系統
- 視圖函數
- ORM(操做數據庫)
- 模板
內容詳細:
1. 請求生命週期
- 網站本質:socket
服務端(網站):
1. 先啓動並監聽:80端口
3. 獲取請求信息:
獲取請求中的URL
根據URL在已經寫好的路由關係中進行匹配:
[
/login/ login
/index/ index
]
def login(request):
請求頭
請求體
處理請求
return
響應內容:
響應頭\r\n\r\n<!DOCTYPE html><html lang="zh-cn">。。。</html>
網站只能看到相應體,在network中能夠看到響應頭,服務器在給瀏覽器點東西,至關於令牌之類的
下次知道你是你。
請求,響應成對出現
重定向,服務器將內容發送到瀏覽器,瀏覽器在去。
本質給瀏覽器字符串,服務器的html文件就是個模版
斷開
客戶端(瀏覽器):
https://www.cnblogs.com/news/
2. 發送請求
- 鏈接:www.cnblogs.com:80
- 發送數據:
GET:
GET /news/?page=1&xx=11 http1.1\r\n
host:www.cnblogs.com\r\n
Accept-Encoding:gzip, deflate, br\r\n
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
\r\n\r\n
POST:
POST /news/?page=1&xx=11 http1.1\r\n
host:www.cnblogs.com\r\n
Accept-Encoding:gzip, deflate, br\r\n
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
\r\n\r\n
name=alex&pwd=123
post能夠兩個位置都有數據,2選一
3. 接收響應內容
響應頭悄悄寫到瀏覽器
響應體在瀏覽器上展現
斷開
2. 主機管理:部門管理
static靜態文件夾,裏面有本身的js,css,img便於分類,再建立plugins文件夾,放入外部引用的插件,好比bootstrap
- 建立procjet和app
- 修改配置文件:
1. 配置
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
2. 模板路徑
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
3. 靜態文件目錄
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
- 寫代碼:
urls.py
from app01 import views
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^login/', views.login),
url(r'^index/', views.index),
]
views.py
def login(request):
if request.method == "GET": # 請求方法
request.GET
else:
request.GET # URL中取值
request.POST # 請求體取值
return HttpResponse('...')
return redirect('...')
return render(request,'模板文件路徑',{'k':'v'})
return HttpResponse('歡迎登陸')
def index(request):
return HttpResponse('歡迎登陸')
- 建立用戶表
models.py
from django.db import models
class UserInfo(models.Model):
id = models.AutoField(primary_key=True) #自增主鍵
user = models.CharField(max_length=32) #vchar模式,有辦法設置成char模式
pwd = models.CharField(max_length=64)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',# "app01" #兩種都好使,可是第一種功能比較全。
]
MySQL
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'數據庫名稱', # 記得本身先去建立數據庫
'USER': 'root',
'PASSWORD': 'xxx',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
procjet.procjet.__init__.py
不要用默認mysqldb,換成pymysql鏈接數據庫
import pymysql
pymysql.install_as_MySQLdb()
SQLlite:
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
執行命令:建立數據庫表
python3 manage.py makemigrations
python3 manage.py migrate
- 對應關係
類 -> 表
對象 -> 行
- 數據庫操做總結:
表級別:
from django.db import models
# 類對象數據庫的表
class UserInfo(models.Model):
# 字段對應數據庫中列
id = models.AutoField(primary_key=True) # 建立id列,自增,int,主鍵
user = models.CharField(max_length=32,null=False) # varchar(32)
pwd = models.CharField(max_length=64)
age = models.IntegerField() # int類型
class Department(models.Model):
"""
部門表
"""
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
python manage.py makemigrations
python manage.py migrate
行:
增長:
models.UserInfo.objects.create(user="alex",pwd='123',age=18)
dic = {'user':'alex','pwd':'123',"age":18 }
models.UserInfo.objects.create(**dic)
刪除:
models.UserInfo.objects.filter(id=12,name='alex').delete()
dic = {'user':'alex','pwd':'123',"age":18 }
models.UserInfo.objects.filter(**dic).delete()
修改:
models.UserInfo.objects.filter(id=12,name='alex').update(user='11111',pwd='xxxx')
models.UserInfo.objects.filter(**{...}).update(**{...})
查看:
# [obj,obj,obj,....]
v = models.UserInfo.objects.all()
# [obj,obj,obj,....]
v = models.UserInfo.objects.filter(....)
# obj
models.UserInfo.objects.filter(....).first()
# 獲取一個對象;無、多都會報錯
models.UserInfo.objects.get(id=1,name='alex')
3. 知識點
1. 路由系統
a. url添加起始和終止符
url(r'^example/$', views.example),
url(r'^example/add/$', views.example_add),
b. 傳參
# 方式一
# http://127.0.0.1:8000/example_edit/?nid=11&xid=1
url(r'^example_edit/$', views.example_edit)
def example_edit(request):
nid = request.GET.get('nid')
xid = request.GET.get('xid')
print(nid,xid)
return HttpResponse('編輯')
# 方式二:
\d 表示數字,$表示結束 +表示一個或多個
# /example_edit/123/111/
url(r'^example_edit/(\d+)/(\d+)/$', views.example_edit),
def example_edit(request,nid,xid):
print(nid,xid)
return HttpResponse('編輯')
### 在 url(r'^host_edit/(\d+)/$', views2.host_edit), 的狀況下,
<form class="form-inline" action="/host_edit/{{ id }}/" method="post"> {#要想訪問函數,必須路徑一致因此將變量id傳進來#},要有/
# 方式三:
# /example_edit/123/111/
url(r'^example_edit/(?P<xid>\d+)/(?P<nid>\d+)/$', views.example_edit),
def example_edit(request,nid,xid):
print(nid,xid)
return HttpResponse('編輯')
c. 路由分發
s19day17/urls.pyt
url(r'^cmdb/', include('cmdb.urls'))
url(r'^openstack/', include('openstack.urls'))
cmdb/urls.py
from django.conf.urls import url,include
from cmdb import views
urlpatterns = [
url(r'^host/$', views.host),
]
openstack/urls.py
from django.conf.urls import url,include
from openstack import views
urlpatterns = [
url(r'^host/$', views.host),
]
欠:
name
namespace
2. 視圖函數
def example_edit(request,nid,xid):
request.method
request.GET
request.POST
return HttpResponse('文本')
return redirect('url')
# 打開模板,讀取數據到內存
# {'k1':'v1'},對模板中的特殊字符進行「渲染」
# 生成不含特殊標籤(已經被替換完畢)的字符串
若是{}字典當中是字符串,能夠用「」轉換,不然容易報錯
return render(request,'模板路徑',{'k1':'v1'})
3. 模板語言
a. 單值
{{ msg }}
b. 索引 .
c. 循環
d. 判斷
e. 母板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css">
<style>
body{
margin: 0;
}
</style>
{% block css %} {% endblock %}
</head>
<body>
<div style="height: 48px;
頭部菜單
</div>
<div>
<div style="float: left;width: 20%;background-color: #dddddd;height: 500px;">
菜單
</div>
<div style="float: left;width: 80%">
{% block content %} {% endblock %}
</div>
</div>
{% block js %} {% endblock %}
</body>
</html>
{% extends "layout.html" %}
{% block content %}
<h1>編輯部門</h1>
<form method="post">
<div class="form-group">
<label for="t1">部門名稱</label>
<input id="t1" type="text" class="form-control" name="title" value="{{ obj.title }}">
</div>
<input type="submit" value="修改" class="btn btn-default">
</form>
{% endblock %}
{% block js %}
<script></script>
{% endblock %}
欠:模板中函數
- simple_tag
- filter
- inlustion_tag
4. 補充:FK
增長列的時候,須要指定值,不然爲空的話會報錯。
foreign key 會自動在後麪價格_id
depart = models.ForeignKey(to='Department',to_field='id',default=1)
關聯Department的id字段和本身。css