49๐
If the file is static (not generated by the django app) then you can put it in the static directory.
If the content of this file is generated by Django then you can return it in a HttpResponse with text/plain
as 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.
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
9๐
I agree with @luc, however another alternative is to use X-Accel-Redirect
header.
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]-How secure is Django
- [Django]-Actions triggered by field change in Django
- [Django]-Django + WebKit = Broken pipe
7๐
I was using a more complex method until recently, then I discovered this and this:
path('file.txt', TemplateView.as_view(template_name='file.txt',
content_type='text/plain')),
Then put file.txt
in the root of your templates
directory in your Django project.
Iโm now using this method for robots.txt
, a text file like the original asker, and a pre-generated sitemap.xml
(eg, change to content_type='text/xml'
).
Unless Iโm missing something, this is pretty simple and powerful.
- [Django]-Django CSRF check failing with an Ajax POST request
- [Django]-Override existing Django Template Tags
- [Django]-Django, "is not JSON serializable" when using ugettext_lazy?
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:
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.
- [Django]-When will Django support Python 3.x?
- [Django]-Resize fields in Django Admin
- [Django]-How to resolve "iterator should return strings, not bytes"