Django 上傳文件

對(duì)于Web應(yīng)用程序,以便能夠上傳文件(資料圖片,歌曲,PDF格式,文字......),它通常是很有用的。讓我們?cè)谶@一節(jié)中來(lái)討論如何使用Django上傳文件。

上傳圖片

在開(kāi)始開(kāi)發(fā)圖片上傳之前,請(qǐng)確保Python的圖像庫(kù)(PIL)已經(jīng)安裝?,F(xiàn)在來(lái)說(shuō)明上傳圖片,讓我們創(chuàng)建一個(gè)配置文件格式,在 myapp/forms.py -

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
#-*- coding: utf-8 -*-
 from django import forms
 class ProfileForm(forms.Form):
    name = forms.CharField(max_length = 100)
    picture = forms.ImageFields()

正如你所看到的,這里的主要區(qū)別僅僅是 forms.ImageField。ImageField字段將確保上傳的文件是一個(gè)圖像。如果不是,格式驗(yàn)證將失敗。

現(xiàn)在,讓我們創(chuàng)建一個(gè) “Profile” 模型,以保存上傳的資料。在 myapp/models.py -

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
from django.db import models
 class Profile(models.Model):
    name = models.CharField(max_length = 50)
    picture = models.ImageField(upload_to = 'pictures')
    class Meta:
       db_table = "profile"

正如所看到的模型,ImageField 使用強(qiáng)制性參數(shù):upload_to. 這表示硬盤(pán)驅(qū)動(dòng)器,圖像保存所在的地方。注意,該參數(shù)將被添加到 settings.py文件中定義的MEDIA_ROOT選項(xiàng)。

現(xiàn)在我們有表單和模型,讓我們來(lái)創(chuàng)建視圖,在 myapp/ views.py -

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
#-*- coding: utf-8 -*-
 from myapp.forms import ProfileForm
 from myapp.models import Profile
 def SaveProfile(request):
    saved = False
    if request.method == "POST":
       #Get the posted form
       MyProfileForm = ProfileForm(request.POST, request.FILES)
       if MyProfileForm.is_valid():
          profile = Profile()
          profile.name = MyProfileForm.cleaned_data["name"]
          profile.picture = MyProfileForm.cleaned_data["picture"]
          profile.save()
          saved = True
    else:
       MyProfileForm = Profileform()
 
    return render(request, 'saved.htmll', locals())

這部分不要錯(cuò)過(guò),創(chuàng)建一個(gè)ProfileForm 并做了一些修改,添加了第二個(gè)參數(shù):request.FILES. 如果不通過(guò)表單驗(yàn)證會(huì)失敗,給一個(gè)消息,說(shuō)該圖片是空的。

現(xiàn)在,我們只需要saved.htmll模板和profile.htmll模板,表單和重定向頁(yè)面?

myapp/templates/saved.htmll ?

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
<html>
    <body>
       {% if saved %}
          <strong>Your profile was saved.</strong>
       {% endif %}
       {% if not saved %}
          <strong>Your profile was not saved.</strong>
       {% endif %}
    </body>
 </html>

myapp/templates/profile.htmll ?

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
<html>
    <body>
       <form name = "form" enctype = "multipart/form-data" 
          action = "{% url "myapp.views.SaveProfile" %}" method = "POST" >{% csrf_token %}
          <div style = "max-width:470px;">
             <center> 
                <input type = "text" style = "margin-left:20%;" 
                placeholder = "Name" name = "name" />
             </center>
          </div>
 
          <br>
          <div style = "max-width:470px;">
             <center> 
                <input type = "file" style = "margin-left:20%;" 
                   placeholder = "Picture" name = "picture" />
             </center>
          </div>
 
          <br>
          <div style = "max-width:470px;">
             <center> 
                <button style = "border:0px;background-color:#4285F4; margin-top:8%; 
                   height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
                   <strong>Login</strong>
                </button>
             </center>
          </div>
       </form>
    </body>
 </html>

接下來(lái),我們需要配對(duì)網(wǎng)址以開(kāi)始: myapp/urls.py

# Filename : example.py
# Copyright : 2020 By Nhooo
# Author by : www.soo66.com
# Date : 2020-08-08
from django.conf.urls import patterns, url
 from django.views.generic import TemplateView
 urlpatterns = patterns(
    'myapp.views', url(r'^profile/',TemplateView.as_view(
       template_name = 'profile.htmll')), url(r'^saved/', 'SaveProfile', name = 'saved')
 )

當(dāng)訪問(wèn)"/myapp/profile",我們會(huì)得到下面 profile.htmll 模板顯示 ? profile.htmll 模板顯示

在格式提交后,已保存的模板將顯示如下 ? 提交模版頁(yè)面

這里我們只講解圖片上傳示例,但如果想上傳其他類型的文件,只需更換 ImageField 在這兩個(gè)模型及 FileField 表單。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清