如何解决Django在HttpResponse中返回图像和数据
我有一个Django应用程序作为Android应用程序的后端。问题是Android应用程序将图像发送到Django服务器。然后服务器进行一些计算,我需要返回修改后的图像和在服务器中计算出的浮点值作为响应。
很清楚如何使用HttpResponse仅返回图像:
def backend_function(request):
img = request.FILES["file"].read()
img_res,float_res = some_calculations(img)
return HttpResponse(img_res,content_type="image/jpeg")
但是我不知道如何将float_res添加到HttpResponse中。
解决方法
您可以将foo2
附加到返回的#include <iostream>
#include <string>
#include <vector>
// EventHandler and EventEmitter are just included to display my intent
class EventHandler {
public:
virtual ~EventHandler(){}
virtual void HandleEvent(/*some event*/) = 0;
};
class EventEmitter {
public:
std::vector<EventHandler*> handlers;
void AddHandler(EventHandler *handler){
this->handlers.push_back(handler);
}
void EmitEvent(/*some event*/){
for(size_t i = 0; i < this->handlers.size(); i++){
this->handlers.at(i)->HandleEvent(/*some event*/);
}
}
};
// The problem arises in Foo/FooEventHandler with circular references
class Foo {
public:
// This object is designed to carry Foo to the EventEmitter
class FooEventHandler : public EventHandler {
public:
Foo &foo;
FooEventHandler(Foo &foo)
:EventHandler(),foo(foo)
{
printf("FooEventHandler CONSTRUCTOR\n");
}
FooEventHandler(const FooEventHandler &event_handler)
:EventHandler(),foo(event_handler.foo)
{
printf("FooEventHandler COPY\n");
}
FooEventHandler operator=(const FooEventHandler& event_handler) {
printf("FooEventHandler =\n");
this->foo = event_handler.foo;
}
~FooEventHandler(){
printf("FooEventHandler DESTRUCTOR\n");
}
void HandleEvent(/*some event*/){
this->foo.HandleSomeEvent();
}
};
// Foo is just some generic object with a custom handler to ref itself
FooEventHandler event_handler;
Foo(std::string name)
:event_handler(*this)
{
printf("Foo CONSTRUCTOR\n");
}
Foo(const Foo &foo)
:event_handler(foo.event_handler)
{
printf("Foo COPY\n");
}
Foo operator=(const Foo& foo)
{
printf("Foo =\n");
this->event_handler = foo.event_handler;
}
~Foo(){
printf("Foo DESTRUCTOR\n");
}
void HandleSomeEvent(/*some event*/){
printf("Look at me handling an event");
}
};
int main()
{
printf("Foo1 create\n");
Foo foo1("a");
printf("Foo2 equal\n");
Foo foo2("b");
// start infinite loop of ='s
foo2 = foo1;
}
或响应标头或会话中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。