0

I am currently uploading a file to the server in Django as follows (For some unrelated reasons, I cannot use models for this form):

<div class="input-group" style="padding-top: 10px">
    <div><input class="form-control" id="filename" value="Select a ZIP file to upload..." disabled></div>
    <div class="input-group-btn"><input type="button" id="get_file" class="btn btn-primary" value="Browse"
                                                    style="margin-right: 3px;margin-left: 3px"></div>
    <div class="input-group-btn"><input type="submit" id="upload"
         class="btn btn-success" value="Upload" style="display: none"></div>
   <input type="file" id="upload_file" name="upload_file" accept=".zip">
</div>

And then I some JavaScript as:

document.getElementById('get_file').onclick = function() {
     document.getElementById('upload_file').click();
};

$('input[type=file]').change(function (e) {
     document.getElementById('filename').value = ($(this).val());
     document.getElementById('upload').style.display = "inline"
});

document.getElementById('upload').onclick = function() {
};

This works fine and I can upload the file just fine. However, now along with a file, I also want to send a string identifier. I am not sure how I can insert another request parameter on submit from this template?

The Django side looks like:

def upload(request):
    """
    if request.method == 'POST' and request.FILES['upload_file']:
        # Do things

So I need to add another parameter, so that I can do something like: request.GET.get('identifier') and this identifier key/value is inserted in the template code.

2
  • Hi Luca. I happened to notice on your profile that you prefer to write titles in all-lower-case, presumably for stylistic reasons. Could I trouble you to use sentence case instead? There are a good number of editors on the site, who volunteer their time to tidy things up and to make them as readable as possible, but it would help them (and me) enormously if the amount of new work created can be reduced! Thanks. Commented Apr 14, 2017 at 21:10
  • 1
    @halfer Of course. Sorry about that! Commented Apr 14, 2017 at 21:38

1 Answer 1

1

Does adding a hidden input inside the form work?

<input type="hidden" name="identifier" value="..."/>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. Unfortunately, the request.GET.get('studyid') in my view still returns None. I added: <input type="hidden" name="studyid" value="Test"/>
As I was writing this, I realized how stupid i am! It should be request.POST.get(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.