如何从Django提供文本文件?

时间:2022-08-02 16:56:00

I am writing a Django based website, but need to serve a a simple text file. Is the correct way to so this by putting it in the static directory and bypassing Django?

我正在写一个基于Django的网站,但需要提供一个简单的文本文件。这是通过将它放在静态目录中并绕过Django的正确方法吗?

3 个解决方案

#1


28  

If the file is static (not generated by the django app) then you can put it in the static directory.

如果文件是静态的(不是由django应用程序生成的),那么您可以将其放在静态目录中。

If the content of this file is generated by Django then you can return it in a HttpResponse with text/plain as mimetype.

如果此文件的内容是由Django生成的,那么您可以在HttpResponse中将text / plain作为mimetype返回。

content = 'any string generated by django'
return HttpResponse(content, content_type='text/plain')

You can also give a name to the file by setting the Content-Disposition of the response.

您还可以通过设置响应的Content-Disposition为文件命名。

filename = "my-file.txt"
content = 'any string generated by django'
response = HttpResponse(content, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
return response

#2


7  

I agree with @luc, however another alternative is to use X-Accel-Redirect header.

我同意@luc,但另一种选择是使用X-Accel-Redirect标头。

Imagine that you have to serve big protected (have to login to view it) static files. If you put the file in static directory, then it's access is open and anybody can view it. If you serve it in Django by opening the file and then serving it, there is too much IO and Django will use more RAM since it has to load the file into RAM. The solution is to have a view, which will authenticate a user against a database, however instead of returning a file, Django will add X-Accel-Redirect header to it's response. Now since Django is behind nginx, nginx will see this header and it will then serve the protected static file. That's much better because nginx is much better and much faste at serving static files compared to Django. Here are nginx docs on how to do that. You can also do a similar thing in Apache, however I don't remember the header.

想象一下,您必须提供大型受保护(必须登录才能查看)静态文件。如果您将文件放在静态目录中,那么它的访问权限是打开的,任何人都可以查看它。如果你通过打开文件然后提供它来在Django中提供它,那就有太多的IO而Django会使用更多的RAM,因为它必须将文件加载到RAM中。解决方案是有一个视图,它将根据数据库对用户进行身份验证,但是Django不会返回文件,而是将X-Accel-Redirect标头添加到它的响应中。现在,由于Django落后于nginx,nginx将看到此标头,然后它将提供受保护的静态文件。这要好得多,因为与Django相比,nginx在提供静态文件方面要好得多。以下是关于如何做到这一点的nginx文档。您也可以在Apache中执行类似的操作,但我不记得标题。

#3


3  

I had a similar requirement for getting a text template for a form via AJAX. I choose to implement it with a model based view (Django 1.6.1) like this:

我有类似的要求通过AJAX获取表单的文本模板。我选择使用基于模型的视图(Django 1.6.1)来实现它,如下所示:

from django.http import HttpResponse
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin

from .models import MyModel

class TextFieldView(SingleObjectMixin, View):
    model = MyModel

    def get(self, request, *args, **kwargs):
        myinstance = self.get_object()
        content = myinstance.render_text_content()
        return HttpResponse(content, content_type='text/plain; charset=utf8')

The rendered text is quite small and dynamically generated from other fields in the model.

渲染文本非常小,可以从模型中的其他字段动态生成。

#1


28  

If the file is static (not generated by the django app) then you can put it in the static directory.

如果文件是静态的(不是由django应用程序生成的),那么您可以将其放在静态目录中。

If the content of this file is generated by Django then you can return it in a HttpResponse with text/plain as mimetype.

如果此文件的内容是由Django生成的,那么您可以在HttpResponse中将text / plain作为mimetype返回。

content = 'any string generated by django'
return HttpResponse(content, content_type='text/plain')

You can also give a name to the file by setting the Content-Disposition of the response.

您还可以通过设置响应的Content-Disposition为文件命名。

filename = "my-file.txt"
content = 'any string generated by django'
response = HttpResponse(content, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
return response

#2


7  

I agree with @luc, however another alternative is to use X-Accel-Redirect header.

我同意@luc,但另一种选择是使用X-Accel-Redirect标头。

Imagine that you have to serve big protected (have to login to view it) static files. If you put the file in static directory, then it's access is open and anybody can view it. If you serve it in Django by opening the file and then serving it, there is too much IO and Django will use more RAM since it has to load the file into RAM. The solution is to have a view, which will authenticate a user against a database, however instead of returning a file, Django will add X-Accel-Redirect header to it's response. Now since Django is behind nginx, nginx will see this header and it will then serve the protected static file. That's much better because nginx is much better and much faste at serving static files compared to Django. Here are nginx docs on how to do that. You can also do a similar thing in Apache, however I don't remember the header.

想象一下,您必须提供大型受保护(必须登录才能查看)静态文件。如果您将文件放在静态目录中,那么它的访问权限是打开的,任何人都可以查看它。如果你通过打开文件然后提供它来在Django中提供它,那就有太多的IO而Django会使用更多的RAM,因为它必须将文件加载到RAM中。解决方案是有一个视图,它将根据数据库对用户进行身份验证,但是Django不会返回文件,而是将X-Accel-Redirect标头添加到它的响应中。现在,由于Django落后于nginx,nginx将看到此标头,然后它将提供受保护的静态文件。这要好得多,因为与Django相比,nginx在提供静态文件方面要好得多。以下是关于如何做到这一点的nginx文档。您也可以在Apache中执行类似的操作,但我不记得标题。

#3


3  

I had a similar requirement for getting a text template for a form via AJAX. I choose to implement it with a model based view (Django 1.6.1) like this:

我有类似的要求通过AJAX获取表单的文本模板。我选择使用基于模型的视图(Django 1.6.1)来实现它,如下所示:

from django.http import HttpResponse
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin

from .models import MyModel

class TextFieldView(SingleObjectMixin, View):
    model = MyModel

    def get(self, request, *args, **kwargs):
        myinstance = self.get_object()
        content = myinstance.render_text_content()
        return HttpResponse(content, content_type='text/plain; charset=utf8')

The rendered text is quite small and dynamically generated from other fields in the model.

渲染文本非常小,可以从模型中的其他字段动态生成。