An Easy Way To Upload Files To Django
- Upload Any File With Any Size In Any Format
- Saves Files Secure and Denied Foreign Requests
- Simple Import and Usage
- Host Files Safely With Simple Key
$ pip3 install djfiler
# Import Django Filer & json
import json
from djfiler import djfiler
# Initial Django Filer
djs = djfiler.Filer('/dir/of/uploades', True)
''' djs can be any name you like '''
first create the function urls.py
def home(request):
if request.method == "POST":
callback = djs.upload(file=request.FILES['file name uploaded'], name="Optional" )
print(callback) # Its Returns {status:ok | fail,name: name of file ( Its Key Of File ),type: type of file }
Return Any File With A Simple Key (Key Is The String That We Return To You When Uploaded file) then in html file create simple form that send files with a name
<form action="/path/to/any/url" method="post" enctype="multipart/form-data">
Select File to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
then files saves to directory you inital in the Class
urls.py
path('images/<slug:key>', sendfile)
''' path name can be anything you like we just need the key parameters '''
views.py
def sendfile(request, key):
callback = djs.find(key)
if callback != None:
data = json.loads(callback)
if data['find']:
with open(data['uri'], "rb") as f:
return HttpResponse(f.read(), content_type='*/*')
else:
return HttpResponse('ERROR TO SEND FILE')
else:
return HttpResponse('File Not Found')
file.html
<img src="/images/<key>" />
Thats All
Simply Test code like this
$ git clone https://github.com/E-RROR/djfiler
$ cd djfiler
$ python3 test-djfiler.py