启动服务器后第一次上载后,Django上传图像失败

时间:2016-10-16 15:39:49

标签: django file-upload

我有一个Django项目,它通过REST Framework接受图像上传。我第一次从我的应用程序上传图像时工作正常,但所有后续上传图像的尝试都失败了。如果我重新启动服务器,它适用于第一次上传图像,然后无法进行所有后续上传。每次我重新启动服务器它只适用于一次上传。我得到的错误是:

ValueError: The 'photo' attribute has no file associated with it.

完整日志如下所示:

    [16/Oct/2016 17:18:03] "POST /api/photoupload/ HTTP/1.1" 201 399
Internal Server Error: /api/photoupload/
Traceback (most recent call last):
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/rest_framework/viewsets.py", line 87, in view
    return self.dispatch(request, *args, **kwargs)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/rest_framework/mixins.py", line 21, in create
self.perform_create(serializer)
File "/Users/billnoble/Documents/YHistory-Server/items/views.py", line 379, in perform_create
the_photo_url = the_photo.photo.url
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/db/models/fields/files.py", line 68, in _get_url
self._require_file()
File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/db/models/fields/files.py", line 46, in _require_file
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'photo' attribute has no file associated with it.
[16/Oct/2016 17:20:08] "POST /api/photoupload/ HTTP/1.1" 500 17754

我的观看代码如下所示:



class PhotoViewSet(viewsets.ModelViewSet):
    queryset = Photo.objects.all()
    serializer_class = PhotoSerializer
    parser_classes = (MultiPartParser, FormParser,)

    def perform_create(self, serializer):
        #the_itemID = self.request.data['item_id']
       
        the_username = self.request.user
      
        the_title = self.request.data['title']
        the_item_type = self.request.data['item_type']
        the_longitude = self.request.data['longitude']
        the_latitude = self.request.data['latitude']
        the_timestamp = self.request.data['timestamp']           
        the_parsed_timestamp = datetime.strptime(the_timestamp, "%Y:%m:%d %H:%M:%S")

        the_user = User.objects.get(username=the_username)
        the_item = Item.objects.get(pk=2)           

        # Create the photo record with the uploaded photo
        the_photo = serializer.save(user=the_user,
                                    item=the_item,
                                    photo=self.request.data.get('photo'),
                                    photo_thumbnail=self.request.data.get('photo'))
        the_photo_url = the_photo.photo.url
        
        # Now create the Item record using the other parameters and the info from the photo record
        the_item = Item.objects.create(url=the_photo_url,
                                       owner=the_user,
                                       item_type=the_item_type,
                                       title=the_title,
                                       active = 1,
                                       lat = the_latitude,
                                       long = the_longitude,
                                       date_taken = the_parsed_timestamp)
        
        the_item.save()

        # Put the real item id in the Photo table
        the_photo.item = the_item
        the_photo.save()




我的模型看起来像这样:



class Photo(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    photo = models.ImageField(upload_to="userphotos/",
                              height_field=None,
                              width_field=None,
                              max_length=100,
                              default='/none/')

    photo_thumbnail = ProcessedImageField(upload_to='userthumbs/',
                                processors=[ResizeToFill(400, 400)],
                                format='JPEG',
                                options={'quality': 60}, default='/none/')


    date_submitted = models.DateTimeField(auto_now_add=True)




0 个答案:

没有答案