file upload django
# vi settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#Manage static for IMS
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
--------------------------------------------------------------------------------
# vi urls.py project wale me
path('useraccess/',include('useraccess.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
------------------------------------------------------------
# vi models app wala model hai ye
Create your models here.
class Document(models.Model):
title = models.CharField(max_length = 30, default="")
uploadedFile = models.FileField(upload_to = "useraccess/file",default="")
dateTimeOfUpload = models.DateTimeField(auto_now = True)
----------------------------------------------------------------------------
# vi HTML
<form action="." method="POST" enctype="multipart/form-data"> (Mutipart is important )
{% csrf_token %}
add type=file
----------------------------------------------------------------------
# vi views
def index(request):
if request.method == 'POST' and request.FILES['attachment']:
myfile = request.FILES['attachment']
print(myfile.name)
attch = Document.objects.create(uploadedFile=myfile)
attch.save()
return render(request, 'useraccess/index.html', params)
Comments
Post a Comment