extras-mongoengine
extras-mongoengine copied to clipboard
Missing uploaded file using LocalStorageFileField
I have a Image model using the Document class, with a LocalStorageFileField for store in the filesystem(or S3), when i submit the form i can find the image file in the MEDIA_ROOT folder defined in settings.py
My code in models.py: http://dpaste.com/hold/1531349/
I have the same problem
have same problem here . but i solve this problem by saving image with define image
ex : Post(Document): image_post = LocalStorageFileField(required=False)
p = Post.objects.create(image_post=File) # if we add the file in here it's not store the file to Media p.image_post.save('image_name',File) # this will create the image file on filesystem
i know it's not elegant but it's solved the store problem.
Would it be possible to have source code extract about your models, views, form & template ? because I tried since yesterday and I'm not able to get it to work.
I'm very novice in django & mongodb and it's a bit tricky for me ...
Currently, only filename is stored in DB. You will find my source code below. I'm not sure where I should use your workaround.
If you can help me it would be great :)
My model :
class Upload(Document): name = StringField(max_length=255) upload = LocalStorageFileField(required=True)
def __unicode__(self):
return u'%s' % (self.name)
My form :
class UploaderForm(forms.Form):
upload = forms.FileField()
def clean_upload(self):
upload = self.cleaned_data['upload']
content_type = upload.content_type
if content_type in settings.CONTENT_TYPES:
if upload.size > settings.MAX_UPLOAD_SIZE:
raise forms.ValidationError(('Please keep filesize under %s. Current filesize %s')
% (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(upload.size)))
else:
raise forms.ValidationError(('File type is not supported'))
return upload
My view
def upload(request): response_data = {} if request.is_ajax(): form = UploaderForm(request.POST, request.FILES)
if form.is_valid():
upload = Upload.objects.create(
upload=request.FILES['upload']
)
upload.name = request.FILES['upload'].name
upload.save()
response_data['status'] = "success"
response_data['result'] = "Your file has been uploaded:"
response_data['fileLink'] = "/%s" % upload.upload
return HttpResponse(json.dumps(response_data), content_type="application/json")
response_data['status'] = "error"
response_data['result'] = "We're sorry, but something went wrong. Please be sure that your file respects the upload conditions."
if not request.GET:
return render_to_response('smartrip/addpicture.html', context_instance=RequestContext(request))
return HttpResponseRedirect('smartrip/success')
My template :
<script>
var options = {
type: "POST",
url: 'http://127.0.0.1:8000/smatrip/upload/',
//data: new FormData(document.getElementById('uploadForm')),
data: document.getElementById('uploadForm'),
processData: false,
contentType: false,
error: function(response) {
message = '<span class="error">We\'re sorry, but something went wrong. Retry.</span>';
$('.upload-message').html(message);
$('fileInput').val('');
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
console.log("text status: " + textStatus);
console.log("error: " + err);
},
success: function(response) {
message = '<span class="' + response.status + '">' + response.result + '</span> ';
message = ( response.status == 'success' ) ? message + response.fileLink : message;
$('.upload-message').html(message);
$('fileInput').val('');
}
};
$('#UploadForm').ajaxForm({
beforeSubmit: function(a,f,o) {
$('.Upload-message').html('Submitting...');
},
success: function(data) {
message = 'done ! : '
$('.Upload-message').html(message);
}
});
</script>