1

I have a .html file. I want to access file like this (http://my_site_name.com/somefile.html) from my Django website. I do not want to use static folder. thanks

Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44

2 Answers2

1

You can not do that, you should serve it using urls and views or directly using TemplateView.

from django.views.generic import TemplateView

urlpatterns = [
     (r'^somefile/$', TemplateView.as_view(template_name='somefile.html')),
]
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
1

If you really do not want to use static folder, why not just bind this file to a view function , treat it as a template, and write route for it? You can achieve this by using TemplateView.

If you do not want to touch template either, there have another solution.

from django.contrib import admin
from django.urls import path
from django.http import HttpResponse
from django.conf import settings


def staticView(request):
    with open(settings.BASE_DIR+"/a.html") as fp:
        return HttpResponse(fp.read())

urlpatterns = [
    path('admin/', admin.site.urls),
    path('a.html',staticView)
]

As you can see, codes above just read a html file, and make HTTP response using file contents. No Template or static folder, you can put your file into any directory which can be accessed.

H.C.Liu
  • 156
  • 1
  • 4