是否可以在视图 django 中添加方法,用于 class 视图中 returns 方法 return 字符串中的 httpresponse

is it possible to add method in views django ,to be used in a class views that returns the method return string in a httpresponse

def 关机(设备): s=device.shutdown() #布尔 return s

class维护(): def get(自我,请求): 设备=request.GET['device'] 好=关机(设备) return Httpresponse(好的,content_type='text/plain')

您认为有几个问题:

  1. 至少,在编写基于 class 的视图时,您可能希望继承 django.views.generic.View

  2. 您没有将 HttpResponse

    中的 R 大写

以下将起作用:

from django.http import HttpResponse
from django.views.generic import View


def shutdown(device):
    s = device.shutdown() #bool
    return s


class Maintain(view):
    def get(self, request):
        device = request.GET['device']
        ok = shutdown(device)
        return HttpResponse(ok, content_type='text/plain')