There is a downloaded folder with files and other folders. Is it possible to visualize it in a template and, if so, how to do it and with what? A, and it should be possible to select a catalog item (or path to it).

  • Why not, the first thing that comes to mind is python to watch the environment (directory) and render html , everything seems to be simple - Igor Lavrynenko
  • @Igor Sergeevich, can you tell me how? on a simple example of some ... if not difficult ... - ddsds
  • It seems that the answer to your question is below, read the work with the file system and rendering in jang, I worked with it a little, there is no ready example - Igor Lavrynenko

1 answer 1

There is a built-in FilePathField

It does not look very nice in the first approximation, but it can fit.

In addition, you can write your widget for a more colorful presentation.

Example of use:

forms.py

 class FileBrowse(forms.Form): afile = forms.FilePathField(label='test', path='./mypath', recursive=True) 

views.py

 def test_filebrowse(request): if request.method == 'POST': form = FileBrowse(request.POST) afile = form.data['afile'] print (afile) return HttpResponse(afile) else: form = FileBrowse() context = {'form': form, } result = render(request, 'site/test.html', context) #print (result.content) return result 

test.html:

 <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>Test file</title> </head> <body> <form action="/filetest" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="send" /> </form> </body> </html> 
  • Help me understand, try your code and get the error FileBrowse 'object has no attribute' cleaned_data '. the program receives the archive, which is unarchived, and I need to deduce the structure of what was unarchived. I know the way to unzip. in your code, you replaced form = FileBrowse (request.POST) with form = FileBrowse (path_to_file), where path_to_file is the path to the directory. and replaced the path to the directory in the form itself. correct my mistakes .... - ddsds
  • Try it like this. Instead of afile = form.cleaned_data ['afile'], you need afile = form.data ['afile']. Corrected in the answer :) - Mikhail Alekseevich
  • now gives the key error = \ says there is no key afile I correctly understand that in this line form = FileBrowse (request.POST), we create the form variable, which we assign the path to the directory? or I did not truly understand the work? - ddsds
  • @ddsds in this case, form is an object from form.py. The key name is the same as in afile = forms.FilePathField - Mikhail Alekseevich
  • thanks a lot, it worked) there is a widget left to correct (I'll try it myself)) - ddsds