miu
miu

Reputation: 199

How to upload list of files to URL via django?

In Django, I was able to get the list of files from the media folder, how to select the list of files and send those files to given URL.

User will select the given Location and push the files the locations.

def todownload(request, dir_name="media"):
   if request.method == 'POST' and 'download' in request.POST:
      files = request.POST.getlist("file_list") 
      print(files)

@ajax_request
def listofilesVIEW(request, *args, **kwargs):
     template_name = 'listofiles.html'
     path = os.path.join(settings.MEDIA_ROOT, dir_name)
     images = []
     for f in os.listdir(path):
          if f.endswith(".img"):
                images.append("%s" % (f))
     return render(request, template_name, {'images': images})

Here is the code for the checkbox:

<div class="row"><div class="col">
{% for image in images %}
 <div class="form-check"><label class="info-title form-check-label"><input class="form-check-input" type="checkbox" value="{{ image }}" />
{{ image }}
{% endfor %}
</div>
</div>

Upvotes: 0

Views: 128

Answers (1)

Syed Jafer
Syed Jafer

Reputation: 306

I will suggest you two ways ,

  1. Create a django model to access the image file . It would be a lot easier in future. Else everytime you have to go the specific location and add the images .
image = models.ImageField(upload_to= "your location",height_field=None, width_field=None, max_length=100)

Ref: Django Image Field Reference

  1. In your current app , while selecting the image checkboxes , identify the name of the file with jquery (javascript) and add them to a array . Then send a POST request to the defined url with this array in the data . So you will be writing a view to handle this .

In Js,

var image_list = [..,..,..,..];
$.post(<url>,{image_list:image_list},function(data,status){
console.log(status)}
);

In view,

images = request.POST.getlist("image_list")

For selecting the selected values from the checkbox ,

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Values of Selected Checboxes</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("button").click(function(){
            var favorite = [];
            $.each($("input[name='sport']:checked"), function(){            
                favorite.push($(this).val());
            });
            alert("My favourite sports are: " + favorite.join(", "));
        });
    });
</script>
</head>
<body>
    <form>
        <h3>Select your favorite sports:</h3>
        <label><input type="checkbox" value="football" name="sport"> Football</label>
        <label><input type="checkbox" value="baseball" name="sport"> Baseball</label>
        <label><input type="checkbox" value="cricket" name="sport"> Cricket</label>
        <label><input type="checkbox" value="boxing" name="sport"> Boxing</label>
        <label><input type="checkbox" value="racing" name="sport"> Racing</label>
        <label><input type="checkbox" value="swimming" name="sport"> Swimming</label>
        <br>
        <button type="button">Get Values</button>
    </form>
</body>
</html>

Upvotes: 1

Related Questions