am very new to concepts of django and it's functionalities so i came across a project which will easily help one to upload a file to a directory using FileField Upload to , but for me it is not working , i tried different ways i modifed it but still i am not sure about the error am getting .
So someone please guide me
here is my code :
Models.py
class UploadedFiles(models.Model):
files_to_upload = models.FileField(upload_to='uploaded_files/', default=None, validators=[validate_file])
path = models.CharField(max_length=100)
server = MultiSelectField(choices=server_list)
SalesforceTicket = models.ForeignKey(SalesforceTicket, related_name="files", on_delete=models.CASCADE)
def __str__(self):
return str(self.SalesforceTicket)
forms.py
class FilesForm(forms.ModelForm):
class Meta:
model = UploadedFiles
fields = ['files_to_upload', 'path', 'server']
# called on validation of the form
def clean(self):
# run the standard clean method first
cleaned_data = super(FilesForm, self).clean()
files_to_upload = cleaned_data.get("files_to_upload")
path = cleaned_data.get("path")
server = cleaned_data.get("server")
# print(files_to_upload)
# print(path)
# print(server)
new_path = path.replace(':', '$', 1)
# print(new_path)
mode = 0o666
for s in server:
s = r'\\' + s
unc_path = os.path.join(s, new_path)
print("hello"+unc_path)
#unc_path = os.mkdir(unc_path, mode)
isdir = os.path.isdir(unc_path)
if isdir:
print("ok")
else:
unc_path = os.mkdir(unc_path, mode)
return cleaned_data
Views.py
def files(request):
num_of_files = 1
filled_multi_file_form = MultipleForm(request.GET)
if filled_multi_file_form.is_valid():
num_of_files = filled_multi_file_form.cleaned_data['num_of_files']
FilesFormSet = formset_factory(FilesForm, extra=num_of_files)
formset = FilesFormSet()
if request.method == 'POST':
filled_form = SnippetForm(request.POST, request.FILES)
filled_formset = FilesFormSet(request.POST, request.FILES)
if filled_form.is_valid() and filled_formset.is_valid():
print"hi"
in settings.py , i included MEDIA_ROOT = 'C:\newform-16oct'
and in html :enctype="multipart/form-data" method="POST"
. Only thing that shows when i search on google is this , and my code is same as the one that is in google , don know whether am not able to identify error .
so after i migrate and run this , it is not throwing me any error , but when i check the folder it is empty
can anyone kindly help me
Error got after editing the code : since the file is not copied to upload_files , when i try to copy the file to another network shared drive it is throwing me error , saying there is no file inside upload_files
so if i try to add using django admin , it is thrwoing me below error
The folder will remain empty since you made a script that generates a new directory, but never do you ask to save that file there.
However I think you are solving the problem at the wrong location. The form does not need to handle that. A Django model can handle this: you can pass a function as value for the upload_to=…
parameter [Django-doc]:
class UploadedFiles(models.Model):
def file_path(self, filename):
return f'uploaded_files/{self.server}/{self.path.replace(":","$", 1)}/{filename}'
files_to_upload = models.FileField(
upload_to=file_path,
default=None,
validators=[validate_file]
)
path = models.CharField(max_length=100)
server = MultiSelectField(choices=server_list)
SalesforceTicket = models.ForeignKey(
SalesforceTicket,
related_name='files',
on_delete=models.CASCADE
)
def __str__(self):
return str(self.SalesforceTicket)
or something similar.