django:从视图返回字符串 [英] django: return string from view

查看:1479
本文介绍了django:从视图返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的问题,对不起。我只想返回一个简单的字符串,没有模板。



我有我的观点:

  def myview(request):
return返回此字符串

我不记得命令了。谢谢

解决方案

根据文档


简单来说,视图函数或视图只是一个Python函数,
接收Web请求并返回Web响应。



每个视图函数负责返回一个HttpResponse
对象。


换句话说,您的视图应该返回一个 HttpResponse 实例:

  from django.http import HttpResponse 

def myview(request):
return HttpResponse(return this string)


I know this is a simple question, sorry. I just want to return a simple string, no templates.

I have my view:

def myview(request):
    return "return this string"

I don't remember the command. Thanks

解决方案

According to the documentation:

A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response.

Each view function is responsible for returning an HttpResponse object.

In other words, your view should return a HttpResponse instance:

from django.http import HttpResponse

def myview(request):
    return HttpResponse("return this string")

这篇关于django:从视图返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆