使用EmberData和Django REST Framework进行POST请求

时间:2016-01-04 09:31:04

标签: django ember.js ember-data django-rest-framework json-api

我正在整合django-rest-framework后端和Ember前端(EmberData)。这些是我的版本:

  • Django 1.9
  • djangorestframework 3.3.2
  • django-rest-framework-json-api(来自github的latest version
  • ember-cli 1.13.13
  • ember 2.2.0
  • ember-data 2.2.1

GET请求工作正常,但POST不是。这是来自EmberData的请求:

{
  "data": {
    "attributes": {
      "name": "The project name",
      "description": "The project description",
      "price": 123
    },
    "relationships": {
      "onwer": {
        "data": null
      }
    },
    "type": "projects"
  }
}

但是django正在崩溃,报告:

Page not found ... The current URL, api/projects, didn't match any of these

我的网址是使用路由器创建的:

from rest_framework.routers import DefaultRouter

from .viewsets.projects import ProjectViewSet


router = DefaultRouter()
...
router.register(r'projects', ProjectViewSet)
...

ViewSet

from rest_framework import viewsets
from rest_framework import filters

from vwrks.models import Project

from ..serializers import ProjectSerializer

class ProjectViewSet(viewsets.ModelViewSet):
    """
    A viewset for viewing and editing Project instances.
    """
    serializer_class = ProjectSerializer
    queryset = Project.objects.all()
    filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter)
    filter_fields = ('seller', 'artist', 'subcategory')
    search_fields = ('name', 'description')

根据文档(http://www.django-rest-framework.org/api-guide/viewsets/#modelviewset),ViewSet提供对.create()的支持,因此应支持此POST操作。

我还调整了Django的尾部斜杠,以便POST接受/projects EmberData /projects/正在做的事情(除了Django期待的默认# EmberData does POST to /projects (not /projects/), so we need this: APPEND_SLASH = False 之外)

Django

我看到的问题是project-create的路由器未生成project-list网址(仅列出project-detail^api/ ^projects/$ [name='project-list'] ^api/ ^projects\.(?P<format>[a-z0-9]+)/?$ [name='project-list'] ^api/ ^projects/(?P<pk>[^/.]+)/$ [name='project-detail'] ^api/ ^projects/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='project-detail'] ):

POST

如何才能将EmberData drf的请求从brew update brew upgrade brew install go --cross-compile-common 发送到brew upgrade

1 个答案:

答案 0 :(得分:3)

不要注意Django消息:

#include<iostream>
#include<iomanip>
#include <limits>  // only for std::numeric_limits<std::streamsize>::max()


const int students = 3;
const int grades = 5;
const int MIN_GRADE = 0;
const int MAX_GRADE = 100;
constexpr size_t SS_MAX = std::numeric_limits<std::streamsize>::max(); // You can use SS_MAX = 128 or something else

void FillGrades( int[][grades], int, int);
void PrintGrades( int[][grades], int, int);
void GetFinal( int[][grades], double[students], int, int);
void GetChar( double [students], char[students], int);

int main() {
    int CSC212[students][grades];
    double FinalValue[students];
    char FinalChar[students];

    FillGrades(CSC212, students, grades);
    PrintGrades(CSC212, students, grades);
    GetFinal(CSC212, FinalValue, students, grades);
    GetChar(FinalValue,FinalChar, students);

    return 0;
}

void FillGrades( int setofgrades[][grades], int pupils, int tests ) {

    for ( int i = 0; i < pupils; ++i ) {
        std::cout << "Please, enter grades for student " << (i + 1) << ":\n";
        for ( int j = 0; j < tests; ++j ) {
            int grade;
            std::cout << "Enter Grade " << (j + 1) << ": ";
            std::cin >> grade;
            while ( std::cin.fail() || grade < MIN_GRADE || grade > MAX_GRADE ) { 
                std::cout << "Wrong value, please enter a number between " << MIN_GRADE << " and " << MAX_GRADE << ": ";
                std::cin.clear();
                std::cin.ignore(SS_MAX,'\n'); // ignore what remains in buffer
                std::cin >> grade;
            }
            setofgrades[i][j] = grade;
            std::cin.ignore(SS_MAX,'\n'); // only one value per line entered
        }

    }
}

void PrintGrades( int setofgrades[][grades], int pupils, int tests ) {
    std::string border(45,'-');

    std::cout << '\n' << border << '\n';
    std::cout << std::setw(5) << "Std" << std::setw(8) << "Att" << std::setw(8) << "Ass" << std::setw(8) << "EI" << std::setw(8) << "EII" << std::setw(8) << "FE\n";
    std::cout << border << '\n';

    for( int i = 0; i < pupils; i++ ) {
        std::cout << std::setw(5) << (i + 1);
        for( int j = 0; j < tests; j++ ) {
            std::cout << std::setw(8) << setofgrades[i][j];
        }
        std::cout << '\n';
    }
    std::cout << border << '\n';

}

void GetFinal( int setofgrades[][grades], double FinalValue[students], int pupils, int tests) {
    //double FinalValue[students];
    double credits[] = { 0.05, 0.1, 0.2, 0.25, 0.4 };

    for( int i = 0; i < pupils; i++ ) {
        double final = 0;
        for ( int j = 0; j < tests; j++ ) {
            final += setofgrades[i][j] * credits[j];
        }
        FinalValue[i] = final;     
    }
    std::cout << '\n';
    for( int i = 0; i < pupils; i++ ) {    // You don't really need another loop...
        std::cout << "Student " << ( i + 1) << " final grade: " << std::setw(6) << std::setprecision(2) << std::fixed << FinalValue[i] << '\n';
    }   
}

void GetChar( double FinalValue[students], char FinalChar[students], int pupils) {

    std::cout << "\nFinal grades:\n";
    for( int i = 0; i < pupils; i++ ) {
        if ( FinalValue[i] < 60 ) {
            FinalChar[i] = 'F';
        } else if ( FinalValue[i] < 70) {  // what happened to E?
            FinalChar[i] = 'D';
        } else if ( FinalValue[i] < 80) {
            FinalChar[i] = 'C';
        } else if ( FinalValue[i] < 90) {
            FinalChar[i] = 'B';
        } else {
            FinalChar[i] = 'A';
        }
        std::cout << "Student " << ( i + 1 ) << " final grade: " << FinalChar[i] << '\n';
    }
}

相关系统是DRF's router,不受You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8080/api/projects/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings. 的影响。您需要删除尾部斜杠:

APPEND_SLASH