简体   繁体   中英

How to Call a Django Rest Api from the form action method

I want to upload a csv file to the database using Django rest api. My content of html page

  <form class="form-inline " method="POST" action="uploadfile" enctype="multipart/form-data">
                            <div class="form-group col-lg-3 col-sm-5">
                                <label for="selectclient">SELECT CLIENT : </label>
                                <select class="form-control ml-2 w-50" id="selectclient">
                                  <option>Client</option>
                                  <option>USPLIMARA</option>
                                  <option>USPLMSTAKEN</option>
                                  <option>USPLWROGN</option>
                                </select>
                              </div>
                              <div class="form-group col-lg-3 col-sm-3">
                                  <label for="selecttype">Type : </label>
                                  <select class="form-control ml-2 w-50" id="selecttype">
                                    <option>Type</option>
                                    <option>SALES</option>
                                    <option>INVENTORY</option>
                                    <option>RETURNS</option>
                                  </select>
                                </div>
                                <div class="form-group col-lg-3 col-sm-4  ">
                                    <label for="selectdburl">DBURL : </label>
                                    <select class="form-control w-50 ml-2" id="selectdburl" name="dburl">
                                      <option>Dburl</option>
                                      <option>localhost:3306/usplimara</option>
                                      <option>localhost:3306/usplmstaken</option>
                                      <option>localhost:3306/usplwrogn</option>
                                    </select>
                                  </div>
                                  <div class="form-group col-lg-2 col-sm-4  ">
                                      <input type="file" id="uploadfile1" name="missingcsvfile" class="mt-2">
                                      <button type="submit" class="btn btn-outline-primary btn-sm mt-2">Upload file</button>
                                  </div>
                              </form>

I created a Rest Api

urlpatterns = [url(r'^upload/(?P<filename>[^/]+)$', views.FileUploadView.as_view()),]

I don't know how to call the api to access this Api or how to specify it in the action method. Please let me know.

Action attribute tells where the data from the form should be send. One tricky thing, is that you have an URL parameter with a name of the file defined in the URL configuration. So it'd have to be different for every new file. If it has to stay this way, you'd have to use JavaScript to dynamically override action attribute based on the name of the file.

Another solution would be to change your URL configuration so you can upload using /upload without the need of specifying the name in the URL (you can always send the name as a part of the form data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM