An Easy Way To Upload Files To Django
$ 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
Python Engineer Roadmap
a simple blog and otp Authentication with django rest framework
This project is an open-source project which I made due to sharing my experience around the Python programming language.
A personal book library | All the books you want in one place
A minimal clone of Divar
It's actually a service which users can orders food very easily
درگاه اتصال به بانک های ایرانی ( درگاه پرداخت بانک ملی ایران،بانک سامان، بانک ملت، درگاه پرداخت زرین پال و ... ) با استفاده از پایتون
a package for type checking the urls and associated views for django