Python django模板中的文件
我在django模板中尝试显示文件下载链接时遇到问题 信息:Python django模板中的文件,python,django,django-models,django-views,django-templates,Python,Django,Django Models,Django Views,Django Templates,我在django模板中尝试显示文件下载链接时遇到问题 信息: AttributeError: 'NoneType' object has no attribute 'split' File "c:\users\ramin\appdata\local\programs\python\python36-32\Lib\wsgiref\simple_server.py", line 35, in close self.status.split(' ',1)[0], sel
AttributeError: 'NoneType' object has no attribute 'split'
File "c:\users\ramin\appdata\local\programs\python\python36-32\Lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
----------------------------------------
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
models.py:
from django.db import models
class Book(models.Model):
Title = models.CharField(max_length=100)
Writer = models.CharField(max_length=100)
Description = models.TextField()
Image = models.ImageField(default='default.jpg')
File = models.FileField(upload_to='PDF/')
def __str__(self):
return self.Title
views.py:
from django.views.generic.list import ListView
from . import models
class BookListView(ListView):
queryset = models.Book.objects.all()
template_name = 'Book/BookList.html'
模板:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Books List</title>
</head>
<body>
{% for x in object_list %}
<h2><a href="">{{ x.Title }}</a></h2>
<img style="width:100px" src="{{ x.Image.url }}">
<p><a href="{{ x.File.url }}" download="{{ x.Title }}">Download</p>
{% endfor %}
</body>
</html>
{%load static%}
书单
{对象_列表%]中x的百分比}
下载
{%endfor%}
当我单击下载链接时,我可以成功下载文件,但我看到此消息可能是您使用
.txt
文件作为默认图像的问题请尝试以下代码,假设对象名称是该模型的对象:
filename = os.path.basename(object_name.file.name)
response = HttpResponse(object_name.file, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
请参阅Django文档中关于直接发送文件的以下部分:在编写问题时,这是一个键入问题:)主要问题没有解决这是一个奇怪的问题…请尝试在新选项卡中打开链接。我不认为这是一个代码问题simple_server.py