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
Asked
Active
Viewed 1,592 times
1
-
[Similar question](https://stackoverflow.com/questions/17989341/) – Alasdair May 02 '18 at 09:01
2 Answers
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
-
my file has static content and never change (and no need to render as html), is there any way to not use template? – Erfan Eghterafi May 02 '18 at 08:57
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