【问题标题】:django - How to insert the content of uploaded file in template?django - 如何在模板中插入上传文件的内容?
【发布时间】:2014-01-29 13:39:47
【问题描述】:

假设我在 FileField 中存储了一个文本文件。现在我想在网页上显示它的内容。我阅读了 django 模板文档,但没有找到方法。

当然,我可以在我的视图中执行content = f.read() 并将content 传递给模板。有没有更好的办法?谢谢!

better way 表示我可以通过将MyModel.objects.all() 传递给模板来完成工作。我不能read 模板中的文件,但应该有一些技巧。

编辑
我试过了:

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>')

但是我认为等效的方法不起作用:

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>')

我真的很想知道原因。

【问题讨论】:

  • 您可以创建自己的小部件并覆盖 render() 方法,但您也必须读取文件。见docs.djangoproject.com/en/dev/ref/forms/widgets/…
  • @gawel 谢谢,我现在就看
  • @gawel 我想这不是我需要的......
  • 你总是可以定义一个方法来返回文件内容并在你的模板中使用 object.this_method

标签: python django django-templates


【解决方案1】:

你可以为你的类定义一个有FileField的方法,如果你想做的比FileFieldread方法做的更多,例如display_text_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>')

观看次数:

def show_files(request):
    objects = MyModel.objects.all()
    return render_to_response('show_files.html', {'objects': objects},
                              context_instance=RequestContext(request))

模板:

{% for obj in objects %}

 <p>
   file name: {{obj.name}} <br>
   file content: {{obj.display_text_file}}
 </p>

{% endfor %}

对于打开文件的方式,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>')

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']

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']

而且他们都有read方法。

【讨论】:

  • 嗯,终于成功了。我想没有其他方法可以做到这一点,无论如何谢谢!
  • 你最后一次编辑 fp = open(self.html_file.path);return fp.read().replace('\n', '&lt;br&gt;') 有效,但 self.text.file.read().replace('\n', '&lt;br&gt;') 无效。
  • 渲染 html 见此页面:stackoverflow.com/questions/4848611/…
猜你喜欢
  • 2016-04-02
  • 2014-10-28
  • 2021-10-29
  • 1970-01-01
  • 2019-04-25
  • 2020-03-30
  • 1970-01-01
  • 2016-11-25
  • 2020-09-06
相关资源
最近更新 更多