没有模型的Django REST API框架 [英] Django REST API framework without models

查看:50
本文介绍了没有模型的Django REST API框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的html页面,该页面允许我上传图像并将其存储在目录中,而无需使用模型.我的目的是使用REST API框架并显示在REST API中上传的特定图像,即

I created a simple html page which allows me to upload an image and store it in the directory without the use of a model. My aim is to use the REST API framework and display that particular image uploaded in the REST API i.e

{"image": "uploaded_image_by_user"}

如何为该REST API创建POST和GET方法?

How can I create the POST and GET methods for this REST API?

我尝试这样做,但是无法正常工作.有适当的方法吗?或者说,有没有人知道没有模型的创建REST API的基本方法?仅供参考,我是Django的新手

I tried doing it like this but it's not working properly. Is there a proper way to do it? Or rather, does anyone know a basic way of creating a REST API without models? FYI, I'm new to Django

我当前的代码:

views.py

from django.shortcuts import render
from django.db import models
from django.views.generic import View, TemplateView, CreateView
from image_app.forms import ImageForm
from django.contrib.messages.views import SuccessMessageMixin
import requests
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from image_app.serializers import ImageSerializer
from django.http import Http404
import os, sys
from rest_framework.parsers import FileUploadParser
from django.http import HttpResponseRedirect
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.contrib import messages


class BaseView(TemplateView):
    template_name = "base.html"

def upload(request):
    if request.method == 'POST':
        filename = rename()
        uploaded_file = request.FILES['image']
        fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT)
        name = fs.save(uploaded_file.name, uploaded_file)
        messages.success(request, 'Uploaded Image Successfully.')
        return HttpResponseRedirect("base.html")
    return render(request, "insert_image.html")

class ImageList(APIView):

    def get(self, request, format = None):
        file = os.listdir(r'path_name_of_uploaded_image')[0]
        print(file)
        if file:
            images = [{"similar_image": file}]
            serializer = ImageSerializer(images, many = True).data
            return Response({'serializer': serializer}, status = status.HTTP_201_CREATED) 
        else:
            return Response({"similar_image": "Similar images are non existent"}, status = 400)

    def post(self, request):
        parser_classes = (FileUploadParser,)
        file = request.data.get('image', None)
        return Response({"similar_image": file}, status = 200)

serializer.py

from rest_framework import serializers

class ImageSerializer(serializers.Serializer):
    similar_image = serializers.CharField()

推荐答案

使用 ViewSets 并覆盖它的get和post方法 https://www.django-rest-framework.org/api-guide/viewsets/

Use ViewSets and override it's get and post methods https://www.django-rest-framework.org/api-guide/viewsets/

提供此 SomeViewSet.as_view({'get':'list','post':'create'})

其中的get和posts指的是GET和POST方法,而list和create指的是ViewSet中的功能

where get and posts refers to the GET and POST methods, while list and create refers to the functions in your ViewSet

您不必在任何地方提及模型.

You don't have to mention models anywhere.

最后,您可以得到这样的内容 return JsonResponse({"image":"uploaded_image_by_user"})

In the end you can have something like this return JsonResponse({"image": "uploaded_image_by_user"})

JsonResponse作为django中的 .http导入JsonResponse

JsonResponse as from django.http import JsonResponse

这篇关于没有模型的Django REST API框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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