簡體   English   中英

如何使用Django Rest Framework返回HttpResponse?

[英]How can I return an HttpResponse with Django Rest Framework?

我正在構建一個API函數,該函數允許客戶端發送帶有URL參數的GET請求,服務器根據給定的信息接收和處理文件,並返回自定義文件。 好消息是我所有步驟都獨立工作!

除了返回HttpResponse 之外,我能夠在def get_query函數中進行所有工作。 該函數需要一個Queryset響應(我想這是合理的..)。 我想我需要另一個函數,以便可以返回HttpResponse,因此創建了def send_file 我不確定如何調用此函數,現在它只是跳過它。

views.py。

class SubFileViewSet(viewsets.ModelViewSet):

    queryset = subfiles.objects.all()
    serializer_class = SubFilesSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,
                          IsOwnerOrReadOnly,)

    def send_file(self, request):

        req = self.request
        make = req.query_params.get('make')
        model = req.query_params.get('model')
        plastic = req.query_params.get('plastic')
        quality = req.query_params.get('qual')
        filenum = req.query_params.get('fileid')
        hotend = req.query_params.get('hotendtemp')
        bed = req.query_params.get('bedtemp')

        if make and model and plastic and quality and filenum:            

            filepath = subfiles.objects.values('STL', 'FileTitle').get(fileid = filenum)

            path = filepath['STL']
            title =  filepath['FileTitle']    

            '''
            #Live processing (very slow)
            gcode = TheMagic(
                make=make, 
                model=model, 
                plastic=plastic, 
                qual=quality, 
                path=path, 
                title=title, 
                hotendtemp=hotend,
                bedtemp=bed)

            '''
            #Local data for faster testing
            gcode = "/home/bradman/Documents/Programming/DjangoWebProjects/3dprinceprod/fullprince/media/uploads"

            test_file = open(gcode, 'r')
            response = HttpResponse(test_file, content_type='text/plain')
            response['Content-Disposition'] = "attachment; filename=%s" % title


            print (response)
            return response

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)        


    def get_queryset(self):
        req = self.request
        make = req.query_params.get('make')
        model = req.query_params.get('model')
        plastic = req.query_params.get('plastic')
        quality = req.query_params.get('qual')
        filenum = req.query_params.get('fileid')
        hotend = req.query_params.get('hotendtemp')
        bed = req.query_params.get('bedtemp')

        return self.queryset
        #function proved in here then removed and placed above
        #get_query required a queryset response

我對Django Rest Framework的經驗很少,我不確定是否有更好的方法來實現這一點,或者不確定如何調用def send_file函數?

您正在尋找自定義路線。 將您的send_file設置為list_route http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

from rest_framework.decorators import list_route

class SubFileViewSet(viewsets.ModelViewSet):
    ...

    @list_route(methods=['get'])
    def send_file(self, request):
        ...

然后您可以通過以下方式訪問此方法

/subfile/send_file/?params

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM