Skip to content
Advertisement

Django: create endpoint for a file

I have a mesh.obj file and I would like to create an endpoint for it. For example, the endpoint could be of the form ‘path/to/mesh’. How would I do this?

Advertisement

Answer

In your urls.py:

url(r'^test-files/(?P<name>.+)/$', views.test_files, name='test_files'),

In your views.py:

from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt # (Allows file download with POST requests, can be omitted)
def test_files(request, name):
    if name == "myxml":
        fsock = open("/djangopath/data/static/files/my.xml", "rb")
        return HttpResponse(fsock)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement