Django模板 render_to_string与render

代码展示:
templates文件夹中的,index.html文件:

Title 这个是从模板中渲染的字符串

urls.py:
from django.urls import path
from front import views
urlpatterns = [
path(’’, views.index),
]

render_to_string:

views.py:
from django.template.loader import render_to_string
from django.http import HttpResponse

def index(request):
html = render_to_string(“index.html”)
return HttpResponse(html)

render:

from django.shortcuts import render

def index(request):
#html = render_to_string(“index.html”)
#return HttpResponse(html)
return render(request,‘index.html’)

猜你喜欢

转载自blog.csdn.net/jiating167168/article/details/89215005