Python Django:如何在不需要任何请求的情况下直接从view to Html页面中的方法加载数据
我有一个方法,它不需要任何请求就将数据发送到HTML页面direclt views.pyPython Django:如何在不需要任何请求的情况下直接从view to Html页面中的方法加载数据,python,html,django,Python,Html,Django,我有一个方法,它不需要任何请求就将数据发送到HTML页面direclt views.py def SendSentence(): sentence = "Hello World" html = render_to_string('Display.html', Context({'Sentence': sentence})) return HttpResponse(html) Display.html <html> <head> </head
def SendSentence():
sentence = "Hello World"
html = render_to_string('Display.html', Context({'Sentence': sentence}))
return HttpResponse(html)
Display.html
<html>
<head>
</head>
<body>
{% csrf_token %}
The data is ...{{ Sentence }}...
</body>
</html>
{%csrf_令牌%}
数据是…{{句子}}。。。
请告诉我这可能吗?或者让我知道如何在调用html页面时直接从view.py获取对该页面的响应:
view函数,简称view,只是一个Python函数,它接受Web请求并返回Web响应
请求是视图函数的必需参数。因此,根据定义,您不能将sendstation()
用作视图
您可以将sendstension()
定义为:
def SendSentence(request):
# do not do anything with request
sentence = "Hello World"
html = render_to_string('Display.html', Context({'Sentence': sentence}))
return HttpResponse(html)
但是,渲染模板的标准方法是:
def SendSentence(request):
sentence = "Hello World"
return render(request, 'Display.html', dict(Sentence=sentence))
没有任何要求?你会发出一个url请求,对吗?是的…我知道如果我们向视图发送请求,那么我们会得到回复。但在我的例子中,我并没有发送请求,我需要将值直接从视图发送到html页面。是否可以在没有任何请求的情况下将数据从视图发送到html页面?