Python django-如何在模板中插入上传文件的内容?
假设我在FileField中存储了一个文本文件。现在我想在网页上显示它的内容。我阅读了django模板文档,但没有找到一种方法来实现这一点 当然,我可以在视图中执行Python django-如何在模板中插入上传文件的内容?,python,django,django-templates,Python,Django,Django Templates,假设我在FileField中存储了一个文本文件。现在我想在网页上显示它的内容。我阅读了django模板文档,但没有找到一种方法来实现这一点 当然,我可以在视图中执行content=f.read(),并将content传递给模板。有更好的办法吗?谢谢大家! better way意味着我可以通过将MyModel.objects.all()传递给模板来完成工作。我不能读取模板中的文件,但应该有一些黑客攻击 编辑 我试过: def display_html(self): content = se
content=f.read()
,并将content
传递给模板。有更好的办法吗?谢谢大家!
better way
意味着我可以通过将MyModel.objects.all()
传递给模板来完成工作。我不能读取模板中的文件,但应该有一些黑客攻击
编辑
我试过:
def display_html(self):
content = self.html_file.read()
print(content)
return content
但是什么也没有显示
最终编辑
很奇怪,下面的代码可以正常工作
class MyModel(models.Model):
name = models.CharField()
text = models.FileField()
def display_text_file(self):
fp = open(self.text.path)
return fp.read().replace('\n', '<br>')
类MyModel(models.Model):
name=models.CharField()
text=models.FileField()
def显示文本文件(自身):
fp=打开(self.text.path)
返回fp.read().replace('\n','
'))
但是,我认为等效的方法不起作用:
class MyModel(models.Model):
name = models.CharField()
text = models.FileField()
def display_text_file(self):
return self.text.read().replace('\n', '<br>')
# neither do self.text.read().decode().replace('\n', '<br>')
类MyModel(models.Model):
name=models.CharField()
text=models.FileField()
def显示文本文件(自身):
返回self.text.read()
#self.text.read()
我真的很想知道原因。如果您想做的不仅仅是读取文件字段的方法,您可以为您的类定义一个具有文件字段的方法,例如显示\u text\u file
,然后在模板中调用它
型号:
class MyModel(models.Model):
name = models.CharField(max_length=100)
text = models.FileField(max_length=100, upload_to='.')
def display_text_file(self):
with open(self.text.path) as fp:
return fp.read().replace('\n', '<br>')
模板:
{% for obj in objects %}
<p>
file name: {{obj.name}} <br>
file content: {{obj.display_text_file}}
</p>
{% endfor %}
self.text
的类型为django.db.models.fields.files.FieldFile
,具有以下方法:
['DEFAULT_CHUNK_SIZE', 'chunks', 'close', 'closed', 'delete', 'encoding', 'field', 'file',
'fileno', 'flush', 'instance', 'isatty', 'multiple_chunks', 'name', 'newlines', 'open', 'path',
'read', 'readinto', 'readline', 'readlines', 'save', 'seek', 'size', 'softspace', 'storage', 'tell',
'truncate', 'url', 'write', 'writelines', 'xreadlines']
['DEFAULT_CHUNK_SIZE', 'chunks', 'close', 'closed', 'encoding', 'file',
'fileno', 'flush', 'isatty', 'mode', 'multiple_chunks', 'name', 'newlines', 'open',
'read', 'readinto', 'readline', 'readlines', 'seek', 'size', 'softspace', 'tell',
'truncate', 'write', 'writelines', 'xreadlines']
self.text.file
的类型为django.core.files.base.file
,并具有以下方法:
['DEFAULT_CHUNK_SIZE', 'chunks', 'close', 'closed', 'delete', 'encoding', 'field', 'file',
'fileno', 'flush', 'instance', 'isatty', 'multiple_chunks', 'name', 'newlines', 'open', 'path',
'read', 'readinto', 'readline', 'readlines', 'save', 'seek', 'size', 'softspace', 'storage', 'tell',
'truncate', 'url', 'write', 'writelines', 'xreadlines']
['DEFAULT_CHUNK_SIZE', 'chunks', 'close', 'closed', 'encoding', 'file',
'fileno', 'flush', 'isatty', 'mode', 'multiple_chunks', 'name', 'newlines', 'open',
'read', 'readinto', 'readline', 'readlines', 'seek', 'size', 'softspace', 'tell',
'truncate', 'write', 'writelines', 'xreadlines']
它们都有read
方法。您可以创建自己的小部件并覆盖render()方法,但也必须读取文件。看@gawel谢谢,我会读的now@gawel我想这不是我需要的…你可以定义一个方法来返回文件内容并使用object。我想没有其他方法可以做到这一点,无论如何,谢谢!最后一次编辑fp=open(self.html\u file.path);返回fp.read().replace('\n','
')
有效,但self.text.file.read().replace('\n','
')
无效。有关呈现html的信息,请参阅此页: