上傳圖片
在python中進(jìn)行圖片操作醉锄,需要安裝包PIL。
pip install Pillow==3.4.1
在Django中上傳圖片包括兩種方式:
- 在管理頁(yè)面admin中上傳圖片
- 自定義form表單中上傳圖片
上傳圖片后浙值,將圖片存儲(chǔ)在服務(wù)器上恳不,然后將圖片的路徑存儲(chǔ)在表中。
創(chuàng)建包含圖片的模型類
將模型類的屬性定義成models.ImageField類型开呐。
1)打開(kāi)booktest/models.py文件烟勋,定義模型類PicTest规求。
class PicTest(models.Model):
pic = models.ImageField(upload_to='booktest/')
2)回到命令行中,生成遷移卵惦。
python manage.py makemigrations
python manage.py migrate
創(chuàng)建一個(gè)管理員,用戶名密碼設(shè)置好阻肿,
python manage.py createsuperuser
注冊(cè)模型類PicTest
from django.contrib import admin
from booktest.models import PicTest
# Register your models here.
admin.site.register(PicTest)
管理界面本地化
本地化是將顯示的語(yǔ)言、時(shí)間等使用本地的習(xí)慣沮尿,這里的本地化就是進(jìn)行中國(guó)化丛塌,中國(guó)大陸地區(qū)使用簡(jiǎn)體中文,時(shí)區(qū)使用亞洲/上海時(shí)區(qū)畜疾,注意這里不使用北京時(shí)區(qū)表示赴邻。
打開(kāi)test1/settings.py文件,找到LANGUAGE_CODE和TIME_ZONE啡捶,賦值姥敛,代碼如下:
LANGUAGE_CODE = 'zh-hans' #使用中國(guó)語(yǔ)言
TIME_ZONE = 'Asia/Shanghai' #使用中國(guó)上海時(shí)間
登錄,輸入賬戶密碼,進(jìn)入管理界面
http://127.0.0.1:8000/admin/
點(diǎn)擊Pic tests后的添加瞎暑,選一個(gè)圖片打開(kāi)彤敛。之后會(huì)在booktest應(yīng)用下看見(jiàn)文件
打開(kāi)test5/settings.py文件,設(shè)置圖片保存路徑了赌。
因?yàn)閳D片也屬于靜態(tài)文件臊泌,所以保存到static目錄下。
MEDIA_ROOT=os.path.join(BASE_DIR,"static/media")`
6)在static目錄下創(chuàng)建media目錄揍拆,再創(chuàng)建應(yīng)用名稱的目錄渠概,此例為booktest。(booktest也可以不建)
自定義form表單中上傳圖片
1)打開(kāi)booktest/views.py文件嫂拴,創(chuàng)建視圖pic_upload播揪。
def pic_upload(request):
return render(request,'booktest/pic_upload.html')
2)打開(kāi)booktest/urls.py文件,配置url筒狠。
url(r'^pic_upload/$', views.pic_upload),
3)在templates/booktest/目錄下創(chuàng)建模板pic_upload.html猪狈。
在模板中定義上傳表單,要求如下:
- form的屬性enctype="multipart/form-data"
- form的method為post
- input的類型為file
<html>
<head>
<title>自定義上傳圖片</title>
</head>
<body>
<form action="/pic_handle/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="pic"> <br>
<input type="submit" value="上傳">
</form>
</body>
</html>
4)打開(kāi)booktest/views.py文件辩恼,創(chuàng)建視圖pic_handle雇庙,用于接收表單保存圖片。
request對(duì)象的FILES屬性用于接收請(qǐng)求的文件灶伊,包括圖片疆前。
from django.conf import settings
from django.http import HttpResponse
...
def pic_handle(request):
"""
上傳圖片的處理
:param request:
:return:
"""
pic = request.FILES.get('pic')
# 返回圖片的名字
print(pic.name)
# 把圖片存入到 static文件夾下的media目錄
# 指定圖片的存儲(chǔ)路徑 static/media/ booktest
save_path = '{}/booktest/{}'.format(settings.MEDIA_ROOT, pic.name)
# 文件進(jìn)行寫入
with open(save_path, 'wb') as f:
for content in pic.chunks():
f.write(content)
# 在數(shù)據(jù)庫(kù)中保存上傳記錄
PicTest.objects.create(pic= 'booktest/{}'.format(pic.name))
return HttpResponse('ok')
5)打開(kāi)booktest/urls.py文件,配置url聘萨。
url(r'^pic_handle/$', views.pic_handle),
6)運(yùn)行服務(wù)器竹椒,在瀏覽器中輸入如下網(wǎng)址:
http://127.0.0.1:8000/pic_upload/
顯示圖片
1)打開(kāi)booktest/views.py文件,創(chuàng)建視圖pic_show米辐。
from booktest.models import PicTest
def pic_show(request):
pic=PicTest.objects.get(id=1)
return render(request, 'booktest/pic_show.html', {'pic':pic})
2)打開(kāi)booktest/urls.py文件胸完,配置url书释。
url(r'^pic_show/$', views.pic_show),
3)在templates/booktest/目錄下創(chuàng)建模板pic_show.html。
<html>
<head>
<title>顯示上傳的圖片</title>
</head>
<body>
<img style="width: 380px ; height: 240px" src="/static/media/{{pic.goods_pic}}"/>
</body>
</html>
4)運(yùn)行服務(wù)器赊窥,在瀏覽器中輸入如下網(wǎng)址:
http://127.0.0.1:8000/pic_show/
點(diǎn)擊上傳之后回顯
修改視圖函數(shù)
def pic_handle(request):
"""
上傳圖片的處理
:param request:
:return:
"""
pic = request.FILES.get('pic')
# 返回圖片的名字
print(pic.name)
# 把圖片存入到 static文件夾下的media目錄
# 指定圖片的存儲(chǔ)路徑 static/media/ booktest
save_path = '{}/booktest/{}'.format(settings.MEDIA_ROOT, pic.name)
# 文件進(jìn)行寫入
with open(save_path, 'wb') as f:
for content in pic.chunks():
f.write(content)
# 在數(shù)據(jù)庫(kù)中保存上傳記錄
pic_file = PicTest.objects.create(pic= 'booktest/{}'.format(pic.name))
return render(request, 'booktest/pic_show.html', {'pic': pic_file})
分頁(yè)
Django提供了數(shù)據(jù)分頁(yè)的類爆惧,這些類被定義在django/core/paginator.py中。 類Paginator用于對(duì)列進(jìn)行一頁(yè)n條數(shù)據(jù)的分頁(yè)運(yùn)算锨能。類Page用于表示第m頁(yè)的數(shù)據(jù)检激。
Paginator類實(shí)例對(duì)象
- 方法init(列表,int):返回分頁(yè)對(duì)象,第一個(gè)參數(shù)為列表數(shù)據(jù)腹侣,第二個(gè)參數(shù)為每頁(yè)數(shù)據(jù)的條數(shù)叔收。
- 屬性count:返回對(duì)象總數(shù)。
- 屬性num_pages:返回頁(yè)面總數(shù)傲隶。
- 屬性page_range:返回頁(yè)碼列表饺律,從1開(kāi)始,例如[1, 2, 3, 4]跺株。
- 方法page(m):返回Page類實(shí)例對(duì)象复濒,表示第m頁(yè)的數(shù)據(jù),下標(biāo)以1開(kāi)始乒省。
Page類實(shí)例對(duì)象
- 調(diào)用Paginator對(duì)象的page()方法返回Page對(duì)象巧颈,不需要手動(dòng)構(gòu)造。
- 屬性object_list:返回當(dāng)前頁(yè)對(duì)象的列表袖扛。
- 屬性number:返回當(dāng)前是第幾頁(yè)砸泛,從1開(kāi)始。
- 屬性paginator:當(dāng)前頁(yè)對(duì)應(yīng)的Paginator對(duì)象蛆封。
- 方法has_next():如果有下一頁(yè)返回True唇礁。
- 方法has_previous():如果有上一頁(yè)返回True。
- 方法len():返回當(dāng)前頁(yè)面對(duì)象的個(gè)數(shù)惨篱。
示例
1)在booktest/views.py文件中創(chuàng)建視圖page_test盏筐。
2)在booktest/urls.py文件中配置url。
url(r'^page(?P<pIndex>[0-9]*)/$', views.page_test),
3)在templates/areatest/目錄下創(chuàng)建page_test.html模板文件砸讳。
4)運(yùn)行服務(wù)器琢融,在瀏覽器中輸入如下網(wǎng)址:
http://127.0.0.1:8000/page/