這裏是新的,我想從媒體文件中提供我的下載文件,並通過管理員上傳文件。我試圖做的東西,我得到這個錯誤在/ 1參數處的TypeError應該是字符串,字節或整數,而不是下載
類型錯誤在/ 1
參數應該是字符串,字節或整數,而不是下載
下面是我的代碼。
view.py
def download(request, download_id):
downloading = Download.objects.get(pk=download_id)
if os.path.exists(downloading):
with open(downloading, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="text/pdf")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(downloading)
response['Content-Length'] = os.path.getsize(downloading)
return response
pdf.closed
raise Http404
url.py
url(r'^(?P<download_id>\d+)$', views.download, name='download'),
HTML鏈接
<a href="{% url 'peruse:download' download.id %}" class="btn btn-generic btn-sm" role="button">DOWNLOAD</a>
model.py
class Download(models.Model):
pdf_name = models.CharField(max_length=500, blank=False)
pdf_file = models.FileField(upload_to='Downloads/%d-%m-%Y/', blank = False,)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
is_visible = models.BooleanField(default = True)
def __str__(self):
return self.pdf_name
和這個視圖處理上傳。
def upload(request):
uploading = Download.objects.filter(is_visible = True)
context = { 'uploading' : uploading }
fillAuthContext(request, context)
return render(request, 'library/resources.html', context)
請幫我瞭解如何使這項工作。謝謝
請格式化您的代碼和整個帖子它真的很難閱讀 – knurzl
格式化謝謝 –
正試圖與我在這裏看到的工作https://stackoverflow.com/questions/36392510/ Django的下載一個文件。請隨時糾正您發現的任何錯誤。我用django不太好。謝謝 –