Python 如何在 Django 中返回静态 HTML 文件作为响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14400035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:18:22  来源:igfitidea点击:

How to return a static HTML file as a response in Django?

pythondjangostatic-html

提问by jjuser19jj

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:

但网站显示的唯一内容是:

>

>

采纳答案by ustun

You are not calling the rendermethod there, are you?

你不是在render那里调用方法,是吗?

Compare:

相比:

template.render

template.render()

回答by César García Tapia

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')

回答by Facundo Casco

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 viewswith the TemplateView, just add a line like this to your urls.py:

对于您的 html,如果它只是一些没有任何动态内容的固定文件,您也可以这样做。您还可以将通用视图TemplateView 一起使用,只需将这样的行添加到您的urls.py

    url(r'^path/to/url', TemplateView.as_view(template_name='index.html')),

回答by Mayur Sable

By using HttpResponseyou can send data only, if you want to html file then you have to use renderor render_to_responseby following ways...

通过使用HttpResponse您只能发送数据,如果您想发送 html 文件,则必须使用renderrender_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')

回答by amigana _34

from django.http import HttpResponse

from django.template import loader

def index(request):

    template=loader.get_template('app/index.html')

    return HttpResponse(template)

回答by Yilmaz

First, you need to do some settings for templating:

首先,您需要对模板进行一些设置:

Create "templates" folder in the rootdirectory, where manage.py is located after you created your project. Then go to settings.py, in the TEMPLATES section enter the directory of templates folder. Hard coding is not recommended because if you send your project to your friend, your friend won't be able to run it. Should be like this:

目录中创建“templates”文件夹,创建项目后 manage.py 位于该文件夹中。然后转到settings.py,在 TEMPLATES 部分输入模板文件夹的目录。不推荐硬编码,因为如果您将项目发送给您的朋友,您的朋友将无法运行它。应该是这样的:

TEMPLATES = [
 {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Assuming that you have home.htmland about.htmlhave in templates folder:

假设您在模板文件夹中有home.htmlabout.html

urls.py

网址.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/',views.aboutview),
    path('',views.homeview),
]

views.py

视图.py

from django.http import HttpResponse
from django.shortcuts import render

def aboutview(request):
  return render(request,"home.html")
def homeview(request):
  return render(request,"about.html")