Django Rest Framework:上传后将文件返回给客户端

时间:2017-03-21 12:23:51

标签: python django file-upload django-rest-framework

我正在尝试将上传的文件返回给客户端。

models.py

file = models.FileField(_('file'), db_index=True, null=True, blank=True, upload_to='files/')

视图

class ContentInfoViewSet(viewsets.ModelViewSet):
    queryset = ContentInfo.objects.all()
    serializer_class = ContentInfoSerializer
    http_method_names = ['get']

    @detail_route(methods=['get']) //this is just for testing
    def files(self, request, pk=None):
        return Response(pk, status=status.HTTP_200_OK)

这里我只是尝试使用“ files ”路线。

当我尝试获取“content-info”时。它工作得很好:

[
  {
    "url": "http://127.0.0.1:8000/users/content-info/1/",
    "id": 1,
    "date": "2017-01-27T16:21:41.976289Z",
    "title": "Hey Hey",
    "image_url": "",
    "content_url": "",
    "file": null
  },
  {
    "url": "http://127.0.0.1:8000/users/content-info/3/",
    "id": 3,
    "date": "2017-03-21T12:09:32.984119Z",
    "title": "NWE",
    "image_url": "",
    "content_url": "",
    "file": "http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf"
  }
]

但该网址不起作用。即使我使用授权进行获取。我不知道我做错了什么。 它没有找到该页面。这是逻辑,因为它不在urls.py中(我的意思是http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf

这个解决方案很棒: pdf

当您打开链接时,它会显示pdf。我认为当我按照“http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf

链接时会发生这种情况

1 个答案:

答案 0 :(得分:0)

AS code22说,我能够用media_root做到这一点,现在它的效果非常好。

我在项目中创建了“media”文件夹。

from proj import settings
from django.views.static import serve

urlpattern = [
    ...
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]

然后,我将其添加到 settings.py

file = models.FileField(_('file'), db_index=True, null=True, blank=True, upload_to='files/')

这是 urls.py (整个项目)

python3 manage.py makemigrations
models.py

中的

python3 manage.py migrate

然后http://api.mydomain.com/media/files/myfile.pdf"summary(groceries) transactions as itemMatrix in sparse format with 57 rows (elements/itemsets/transactions) and 817 columns (items) and a density of 0.03135133 most frequent items: A B C D (Other) 13 13 13 12 12 1397 element (itemset/transaction) length distribution: sizes 3 4 5 6 7 8 9 10 13 14 16 17 18 22 29 30 32 33 34 40 43 45 55 77 86 111 118 353 7 4 4 4 3 4 4 3 1 3 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 Min. 1st Qu. Median Mean 3rd Qu. Max. 3.00 5.00 9.00 25.61 29.00 353.00 includes extended item information - examples: labels 1 E 2 F 3 G groceryrules<-apriori(groceries, parameter = list(support = 0.15, confidence = 0.05, minlen = 2))

文件(上传后)在这里:groceryrules<-apriori(groceries, parameter = list(support = 0.14, confidence = 0.05, minlen = 2)) Apriori Parameter specification: confidence minval smax arem aval originalSupport maxtime support minlen maxlen target ext 0.05 0.1 1 none FALSE TRUE 5 0.14 2 10 rules FALSE Algorithmic control: filter tree heap memopt load sort verbose 0.1 TRUE TRUE FALSE TRUE 2 TRUE Absolute minimum support count: 7 set item appearances ...[0 item(s)] done [0.00s]. set transactions ...[817 item(s), 57 transaction(s)] done [0.00s]. sorting and recoding items ... [14 item(s)] done [0.00s]. creating transaction tree ... done [0.00s]. checking subsets of size 1 2 3 4 5 done [0.00s]. writing ... [90 rule(s)] done [0.00s]. creating S4 object ... done [0.00s]. Warning message: In apriori(groceries, parameter = list(support = 0.14, confidence = 0.05, : Mining stopped (maxlen reached). Only patterns up to a length of 5 returned! 就是这样:)