Search code examples
pythondjangoangularuploader

Angular to Django, passing multiple parameters in a service call


I have some working code on Django, where i created a simple file uploader with a date picker that is passed through a view and then uploaded onto a database using a standalone python class that utilise sqlalchemy.

However, I have been working on migrating the UI aspect onto an Angular Front End, but hit a road block and not sure where I am going wrong. The user needs to be able upload a excel binary file, input a date and hit upload. This should send a request to a view which then handles the request. The view it self works perfectly, when the file and dates are inputted from the Django HTML template. But I can't seem to make it work once the inputs come from Angular.

At the moment I am getting a 'Unsupported Media Type" Error message. But my guess is i am not passing the file and date correctly to the service. Any help would be great

Here is my code

views.py

@api_view(('POST',))
@csrf_exempt
def uploader(request):
    if request.method == 'POST':
        try:
            instance= uploader(request.FILES['data'], request.POST['selectedDate'])
            _ = upload_instance.run_upload_process('data')
            upload_message = "Success"
            return Response(upload_message, status=status.HTTP_201_CREATED)
        except Exception as e:
            upload_message = 'Error: ' + str(e)
            return Response(upload_message, status=status.HTTP_400_BAD_REQUEST)

file-upload.service.ts

DJANGO_SERVER: string = "http://127.0.0.1:8000";

  constructor(private http:HttpClient) { }
  public upload(data: any, selectedDate:any) {
    return this.http.post<any>(`${this.DJANGO_SERVER}/upload/`, data, selectedDate);
  }

upload.component.ts

onChange(event: any) {
    this.filetoUpload = event.target.files[0];
  }

  inputEvent(event:any) {
    this.monthEndDate = event.value;
  }

  onUpload() {
    this.loading = !this.loading;
    this.fileUploadService.upload(this.filetoUpload, this.monthEndDate).subscribe(
      (event: any) => {
        if (typeof (event) === 'object') {
          this.shortLink = event.link;
          this.loading = false;
        }
      }
    )
  }

upload.component.html

 <div class="input-group">
                <input class="form-control" type="file" (change)="onChange($event)">
                <!-- <span><button (click)="onUpload()" class="btn btn-dark">Upload</button></span> -->
            </div>
            <br>
            <div class="input-group" style="font-size: 18px">
                <mat-form-field appearance="outline">
                    <mat-label>Choose a Date</mat-label>
                    <input matInput [matDatepicker]="picker" (dateInput)="inputEvent($event)">
                    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                    <mat-datepicker #picker></mat-datepicker>
                </mat-form-field>
            </div>

Solution

  • Angular http post method receives three parameters

    post(url, data, [config]);
    

    So, you can change your method as follow

      DJANGO_SERVER: string = "http://127.0.0.1:8000";
    
      constructor(private http:HttpClient) { }
      public upload(data: any, selectedDate:any) {
        let postData = {
            data,
            selectedDate
        }
        return this.http.post<any>(`${this.DJANGO_SERVER}/upload/`, postData);
      } 
    

    And in Django server, align your code to properly access post body.