I have not figured out how I can present a website with pure HTML code and/or HTML + JavaScript + CSS.
我还没有弄清楚如何用纯HTML代码和/或HTML + JavaScript + CSS来呈现一个网站。
I tried to load a HTML file with that just says: Hello World.
我试着加载一个HTML文件,上面写着:Hello World。
I know I can do that with Django too, but later on I want to display my website with CSS+JavaScript+HTML.
我知道我也可以用Django来做,但是后来我想用CSS+JavaScript+HTML来显示我的网站。
In the views file I run this code:
在视图文件中,我运行以下代码:
# Create your views here.
from django.http import HttpResponse
from django.template import Context, loader
def index(request):
template = loader.get_template("app/index.html")
return HttpResponse(template.render)
But the only thing the website displays is:
但该网站显示的唯一内容是:
>
>
4 个解决方案
#1
13
You are not calling the render
method there, are you?
你没有调用渲染方法,是吗?
Compare:
比较:
template.render
template.render()
#2
14
If your file isn't a django template but a plain html file, this is the easiest way:
如果您的文件不是django模板,而是一个普通的html文件,这是最简单的方法:
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
#3
6
If your CSS and JS files are static don't use Django to serve them, or serve them as static files
如果您的CSS和JS文件是静态的,不要使用Django来服务它们,或者将它们作为静态文件来服务
For your html you could do the same if it is just some fixed file that won't have any dynamic content. You could also use generic views with the TemplateView, just add a line like this to your urls.py
:
对于您的html,如果它只是一个没有任何动态内容的固定文件,您也可以这样做。你也可以在TemplateView中使用通用视图,在你的url中添加这样的一行。
url(r'^path/to/url', TemplateView.as_view(template_name='index.html')),
#4
5
By using HttpResponse
you can send data only, if you want to html file then you have to use render
or render_to_response
by following ways...
通过使用HttpResponse,您只能发送数据,如果您想要html文件,那么您必须使用呈现或render_to_response,方法如下……
from django.shortcuts import render
def index(request):
return render(request, 'app/index.html')
or
或
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
#1
13
You are not calling the render
method there, are you?
你没有调用渲染方法,是吗?
Compare:
比较:
template.render
template.render()
#2
14
If your file isn't a django template but a plain html file, this is the easiest way:
如果您的文件不是django模板,而是一个普通的html文件,这是最简单的方法:
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
#3
6
If your CSS and JS files are static don't use Django to serve them, or serve them as static files
如果您的CSS和JS文件是静态的,不要使用Django来服务它们,或者将它们作为静态文件来服务
For your html you could do the same if it is just some fixed file that won't have any dynamic content. You could also use generic views with the TemplateView, just add a line like this to your urls.py
:
对于您的html,如果它只是一个没有任何动态内容的固定文件,您也可以这样做。你也可以在TemplateView中使用通用视图,在你的url中添加这样的一行。
url(r'^path/to/url', TemplateView.as_view(template_name='index.html')),
#4
5
By using HttpResponse
you can send data only, if you want to html file then you have to use render
or render_to_response
by following ways...
通过使用HttpResponse,您只能发送数据,如果您想要html文件,那么您必须使用呈现或render_to_response,方法如下……
from django.shortcuts import render
def index(request):
return render(request, 'app/index.html')
or
或
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')