django - 在Django的函数中传递模型的图像属性
问题描述
我有一个名为 的函数detectFace(input_file)
,其中输入文件是一个图像。我想通过views.py
模板呈现结果,但找不到方法。图像将通过模板上传并在detectFace(input_file)
.
像这样的东西: -
def detectFace(input_file):
"""Operation on input_file
"""
pass
现在views.py
我正在尝试创建这样的东西
def face_recog(request):
context = ??
return render(request, 'templates/face.html', context)
我想知道那些问号。我应该如何进行?
解决方案
您可以通过访问访问上传的文件request.FILES['filename']
,然后您可以将该文件传递给您的函数。就像是:
def detectFace(input_file):
"""Operation on input_file
"""
# Some processing
return something
def face_recog(request):
input_file = request.FILES['image_file']
some_data = detectFace(input_file) # call the function
context = {'somekey': some_data} # data returned by detectFace() function.
return render(request, 'templates/face.html', context)
在您的 HTML 中,表单应如下所示:
<form method="POST" enctype='multipart/form-data'>
{% csrf_token %}
<input type="file" name="image_file" />
<input type="submit"></input>
{{ form }}
</form>
推荐阅读
- javascript - 如何使用 mocha 测试私有函数?
- php - PHP:如何搜索数组?
- macos - mac的ctags错误
- function - 仅使用一个内核将 MQL5 函数转换为 OpenCL 内核
- bing - Bing 广告 - 上传离线转化
- c# - 一个视图中的父子模型(多个模型)(ASP.NET Core (C#) + MVC + Entity Framework)
- perl - 通过连接一个字符串和一个 int 将一个变量分配给另一个变量
- paypal - 如果我的服务器端语言没有 SDK,如何在我的网站上使用 PayPal 付款?
- ios - 在 Firebase 的 Tableview 中快速显示数据
- ios - 集合视图单元格不断增加为当前数组计数的两倍