Step1)修改article\models.py,新增附件最多可以上傳五個檔案的資料表。
file1 = models.FileField(upload_to = "upload/", null=True, blank=True)
file2 = models.FileField(upload_to = "upload/", null=True, blank=True)
file3 = models.FileField(upload_to = "upload/", null=True, blank=True)
file4 = models.FileField(upload_to = "upload/", null=True, blank=True)
file5 = models.FileField(upload_to = "upload/", null=True, blank=True)
完整程式如下
Step2)修改article\forms.py,新增附件最多可以上傳五個檔案的表單。
'file1': forms.FileInput(attrs={'class': 'form-control', 'placeholder': '上傳附件1'}),
'file2': forms.FileInput(attrs={'class': 'form-control', 'placeholder': '上傳附件2'}),
'file3': forms.FileInput(attrs={'class': 'form-control', 'placeholder': '上傳附件3'}),
'file4': forms.FileInput(attrs={'class': 'form-control', 'placeholder': '上傳附件4'}),
'file5': forms.FileInput(attrs={'class': 'form-control', 'placeholder': '上傳附件5'}),
完整程式如下
Step3)修改article\admin.py,設定可以管理五個附件。
list_display = ('title', 'content', 'file1', 'file2', 'file3', 'file4', 'file5', 'ctime', 'click', 'user')
完整程式如下
Step4)修改article\template\article_create.html,可以上傳五個附件。
{{ form.file1 }}
{{ form.file2 }}
{{ form.file3 }}
{{ form.file4 }}
{{ form.file5 }}
完整程式如下
Step5)修改article\template\article_update.html,可以更新五個附件。
<div class="form-group">
<label>附件{{ article.file1 }}</label>{{ form.file1 }}
<label>附件{{ article.file2 }}</label>{{ form.file2 }}
<label>附件{{ article.file3 }}</label>{{ form.file3 }}
<label>附件{{ article.file4 }}</label>{{ form.file4 }}
<label>附件{{ article.file5 }}</label>{{ form.file5 }}
</div>
完整程式如下
Step6)修改article\template\article_detail.html,可以下載個別附件。
<label class="list-group-item">附件1:{{ object.file1 }}
<a href="{% url 'article:download' object.file1.path %}">下載</a></label>
<label class="list-group-item">附件2:{{ object.file2 }}
<a href="{% url 'article:download' object.file2.path %}">下載</a></label>
<label class="list-group-item">附件3:{{ object.file3 }}
<a href="{% url 'article:download' object.file3.path %}">下載</a></label>
<label class="list-group-item">附件4:{{ object.file4 }}
<a href="{% url 'article:download' object.file4.path %}">下載</a></label>
<label class="list-group-item">附件5:{{ object.file5 }}
<a href="{% url 'article:download' object.file5.path %}">下載</a></label>
完整程式如下
Step7)修改article\views.py,下載個別附件。
@login_required
def downloadFile(request, filepath):
if filepath != "":
try:
response = StreamingHttpResponse(open(filepath, 'rb'))
response['content_type'] = "application/octet-stream"
response['Content-Disposition'] = 'attachment; filename=' + os.path.basename(filepath)
return response
except Exception:
raise Http404
刪除附件功能
if (a.file1 != ""):
os.remove('{}'.format(a.file1)) # 刪除檔案
if (a.file2 != ""):
os.remove('{}'.format(a.file2)) # 刪除檔案
if (a.file3 != ""):
os.remove('{}'.format(a.file3)) # 刪除檔案
if (a.file4 != ""):
os.remove('{}'.format(a.file4)) # 刪除檔案
if (a.file5 != ""):
os.remove('{}'.format(a.file5)) # 刪除檔案
完整程式如下
Step8)修改article\urls.py,下載個別附件的網址對應。
path('download/(?P<filepath>.*)/$', downloadFile, name='download'),
完整程式如下
Step9)執行結果
點選建立公告,發現可以新增五個附件
點選公告後,每一個附件的右側提供下載按鈕,可以下載附件