2012-11-18 55 views
0

我正在構建一個Django-Tastypie服務器。在我成功創建資源後,服務器將響應我「201創建」,但Content-Type爲text/html。我想要資源總是返回JSON響應,該怎麼做?如何成功創建資源後創建JSON響應?

這裏是我的資源代碼

class UserResource(ModelResource): 

    class Meta: 
     resource_name = "user" 
     queryset = User.objects.all() 
     authentication = Authentication() 
     authorization = Authorization() 

     allowed_methods=["post"] 
     fields= ["username","email","id"] 


    def determine_format(self, request): 
     return "application/json" 

回答

1

嘗試增加

always_return_data = True 

你元。

而當您執行POST時,請確保您發佈的網址末尾有​​。例如。

http://127.0.0.1:8000/api/v1/user/?format=json 

通過捲曲一個POST的完整的示例: -

curl -v -H "Content-Type: application/json" -X POST --data '{"username":"calvin", "email":"[email protected]", "id": "1"}' http://127.0.0.1:8000/api/v1/user/?format=json 
+0

非常感謝你 – user1687717