[英]django - How to insert the content of uploaded file in template?
Say I've stored a text file in FileField. 假设我在FileField中存储了一个文本文件。 Now I want to display its content on the webpage. 现在我想在网页上显示其内容。 I read the django template document but didn't find a way to do this. 我阅读了django模板文档,但没有找到办法。
Of course I can do content = f.read()
in my views and pass content
to the template. 当然,我可以在我的视图中执行content = f.read()
并将content
传递给模板。 Is there a better way? 有没有更好的办法? Thank you! 谢谢!
better way
means I could get the job done by passing MyModel.objects.all()
to template.I can't read
a file in template, but there should be some kind of hacks for this. better way
意味着我可以通过将MyModel.objects.all()
传递给模板来完成工作。我无法read
模板中的文件,但应该有一些黑客。
edit 编辑
I've tried: 我试过了:
def display_html(self):
content = self.html_file.read()
print(content)
return content
but nothing displayed... 但没有显示......
final edit 最后编辑
It's very strange that the following code works 以下代码有效,这很奇怪
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>')
However what I regard as equivalent doesn't work: 然而,我认为相同的东西不起作用:
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>')
I really want to know the reason. 我真的很想知道原因。
You can define a method for your class that have FileField
, if you want to do more than read
method of FileField
do, for for example display_text_file
, and then in template call it. 您可以为具有FileField
的类定义一个方法,如果您想要多做FileField
do的read
方法,例如display_text_file
,然后在模板中调用它。
Models: 楷模:
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>')
Views: 浏览次数:
def show_files(request):
objects = MyModel.objects.all()
return render_to_response('show_files.html', {'objects': objects},
context_instance=RequestContext(request))
Templates: 模板:
{% for obj in objects %}
<p>
file name: {{obj.name}} <br>
file content: {{obj.display_text_file}}
</p>
{% endfor %}
For ways of opening file, in display_text_file
, all of these ways works for me: 对于打开文件的方法,在display_text_file
,所有这些方法对我有用:
def display_text_file(self):
with open(self.text.path) as fp:
return fp.read().replace('\n', '<br>')
def display_text_file(self):
self.text.open() # reset the pointer of file, needs if you need to read file more than once, in a request.
return self.text.read().replace('\n', '<br>')
def display_text_file(self):
self.text.open() # reset the pointer of file, needs if you need to read file more than once, in a request.
return self.text.file.read().replace('\n', '<br>')
Type of self.text
is django.db.models.fields.files.FieldFile
and having following methods: 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']
And type of self.text.file
is django.core.files.base.File
and having following methods: self.text.file
类型是django.core.files.base.File
并具有以下方法:
['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']
And both of them have read
method. 他们俩都有read
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.