Django API使用angular4显示400(错误请求)错误

时间:2018-03-22 08:00:52

标签: django angular

我正在使用Django Rest API并通过Angular4调用它 但是当我尝试上传图片时出现错误错误请求错误

home.component.ts 听说我使用post方法上传带有数据的图像。

import { Component, OnInit } from '@angular/core';
import { Http, Response, Headers} from '@angular/http';


@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private http: Http) { }
  homeObj:object = {};

  addSaveData = function(home){
    this.homeObj = {
      "name" : home.name,
      "email": home.email,
      "subject": home.subject,
      "message": home.message,
      "image" : home.image,
    }
    let _url:string = 'http://127.0.0.1:8000/Home/Homemodel/';
    this.http.post(_url, this.homeObj).subscribe(( res:Response) =>{
      console.log(res);
    })
  }

  ngOnInit() {
  }

}

这是我的 home.component.html

使用angular4代码作为调用Django API的前端。

<div id="headder" class="container"><h3>POST METHOD</h3></div>
<form  class="container" id="formNewPost" name="formNewPost" #postData = "ngForm" (ngSubmit) = "addSaveData(postData.value)">
 <div class="row">
   <div class="col-sm-4">
    <div style="width:100%"><label>Name:</label></div>
    <input style="width:100%" type="text" name="name" id="name" placeholder="name" ngModel>
   </div>


  <div class="col-sm-4">
    <div style="width:100%"><label>EMail:</label></div>
    <input style="width:100%" type="text" name="email" id="email" placeholder="email" ngModel>
  </div>


  <div class="col-sm-4">
    <div style="width:100%"></div><label>Subject:</label>
    <input style="width:100%" type="text" name="subject" id="subject" placeholder="subject" ngModel>
  </div>
  <br>
  <br>
  <div class="col-sm-12">
    <div style="width:100%"><label>Message:</label></div>
    <textarea style="width:100%" type="text" name="message" id="message" placeholder="message" ngModel></textarea>
  </div>
   <input type="file" >
</div><br>
  <input class="btn btn-primary" style="float: right;margin-bottom:15px;" type="submit" value="POST">
</form>

0 个答案:

没有答案