मैं Django में फाइल को अपलोड करने के लिए कैसे झुकाव रहा हूँ "सबमिट किए गए डेटा एक फ़ाइल नहीं था", और यहाँ मैं एक चाहिए होने तुच्छ समस्या का सामना, त्रुटि के साथ:Django बाकी फ्रेमवर्क छवि अपलोड:
The submitted data was not a file. Check the encoding type on the form.
नीचे विवरण है।
नोट: मैं भी Django Rest Framework ImageField को देखा है, और मैं
serializer = ImageSerializer(data=request.data, files=request.FILES)
कोशिश की, लेकिन मैं
TypeError:
__init__()
got an unexpected keyword argument 'files'
मैं एक Image
मॉडल के लिए जो मैं करना चाहते हैं Django रीस्ट फ्रेमवर्क के माध्यम से बातचीत:
models.py
class Image(models.Model):
image = models.ImageField(upload_to='item_images')
owner = models.ForeignKey(
User, related_name='uploaded_item_images',
blank=False,
)
time_created = models.DateTimeField(auto_now_add=True)
serializers.py
class ImageSerializer(serializers.ModelSerializer):
image = serializers.ImageField(
max_length=None, use_url=True,
)
class Meta:
model = Image
fields = ("id", 'image', 'owner', 'time_created',)
settings.py
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser',
),
सामने के छोर (AngularJS का उपयोग कर और angular-restmod
या $resource
) owner
साथ JSON
डेटा भेजने और image
फॉर्म का:
इनपुट:
{u'owner': 5, u'image': u'data:image/jpeg;base64,/9j/4QqdRXhpZgAATU0AKgAAA..."}
लेकिन तब ImageSerializer(data=request.data).errors
{"owner": 5, "image": "data:image/jpeg;base64,/9j/4QqdRXhpZgAATU0A..."}
बैकएंड में, request.data
शो त्रुटि
ReturnDict([('image', [u'The submitted data was not a file. Check the encoding type on the form.'])])
मुझे आश्चर्य है कि क्या मैं त्रुटि को ठीक करना चाहिए पता चलता है?
संपादित करें: जे एस हिस्सा
संबंधित सामने के छोर कोड दो हिस्से होते हैं: एक angular-file-dnd
directive
(उपलब्ध here) पेज और angular-restmod
, जो CRUD आपरेशन प्रदान करता है पर फ़ाइल ड्रॉप करने :
<!-- The template: according to angular-file-dnd, -->
<!-- it will store the dropped image into variable $scope.image -->
<div file-dropzone="[image/png, image/jpeg, image/gif]" file="image" class='method' data-max-file-size="3" file-name="imageFileName">
<div layout='row' layout-align='center'>
<i class="fa fa-upload" style='font-size:50px;'></i>
</div>
<div class='text-large'>Drap & drop your photo here</div>
</div>
# A simple `Image` `model` to perform `POST`
$scope.image_resource = Image.$build();
$scope.upload = function() {
console.log("uploading");
$scope.image_resource.image = $scope.image;
$scope.image_resource.owner = Auth.get_profile().user_id;
return $scope.image_resource.$save();
};
समस्या से संबंधित एक अद्यतन: अभी मैं ng-file-upload
का उपयोग करने के लिए स्विच किया गया है, जो उचित प्रारूप में छवि डेटा भेजता है।
आप फार्म आप पोस्ट करने के लिए प्रयोग कर रहे हैं शामिल करने के लिए अपना कोड अपडेट कर सकते हैं डेटा और कोई प्रासंगिक जेएस? –
@JamieCounsell: मैंने उपरोक्त कोड जोड़े हैं। हालांकि मैंने '
@Lelouch: '" छवि ":" डेटा: image/jpeg; बेस 64,/9j/4QqdRXhpZgAATU0A ... "' जो प्रारूप फ़ाइल यह है ?? –