【问题标题】:failed to sen data to server angularjs无法将数据发送到服务器 angularjs
【发布时间】:2014-03-22 16:44:03
【问题描述】:

我在使用 %http.post 发布新数据时遇到错误。我用 django 在后端创建了一个类别,并且我在字段中写入状态,就像我在 django 后端模型中的选项字段中获得的一样

{"error_message": "Cannot resolve keyword 'category' into field. Choices are: id, inventory, name, slug", "traceback": "Traceback (most recent call last):\n\n "}

我的 django 模型看起来像这样。

from django.db import models

class Inventory(models.Model):
    APPROVAL_CHOICES = (
        (u'Good condition', u'Good condition'),
        (u'Bad condition', u'Bad condition'),
        (u'Broken', u'Broken'),
    )
    name         = models.CharField(max_length=255,  help_text="Enter inventory name")
    slug         = models.SlugField(unique=True, max_length=255)
    description  = models.CharField(max_length=255,  help_text="Enter inventory description")
    count        = models.IntegerField(max_length= 255, help_text="Enter count of Inventory", default=1)
    category     = models.ForeignKey('Category',  help_text="Enter category")
    location     = models.CharField(max_length=255, help_text="Enter inventory location")
    status       = models.CharField(max_length=20, choices=APPROVAL_CHOICES,  help_text="Enter inventory statuss")
    published    = models.BooleanField(default=True)
    created      = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u'%s' % self.name


class Category(models.Model):
    name         = models.CharField(max_length=255,  help_text="Category name")
    slug         = models.SlugField(unique=True, max_length=255)

    def __unicode__(self):
        return u'%s' % self.name

使用 angularjs 我正在尝试使用tastepie api 向服务器发布请求

$scope.createInventory = function(){
        var data = {"name":$scope.newinfo, "description":$scope.newinfo,"count":$scope.newinfo, "location":$scope.newinfo, "category":$scope.newinfo, "status":$scope.newinfo};
        $http.post("http://api.bos.lv/api/v1/inventory/?format=json", data).success(function (data, status, headers) {
            alert("Inventory Added!");
                $http.get(headers("location")).success(function(data){
                    $scope.info.push(data);
                });
        });
    };

那我就用这个表格

<td><input type="text" ng-model="newinfo.name"></td>
    <td><input type="text" ng-model="newinfo.slug"></td>
    <td><input type="text" ng-model="newinfo.description"></td>
    <td><input type="text" ng-model="newinfo.location"></td>
    <!--<td><input type="text" ng-model="newinfo.count"></td>--->
     <td><select ng-model="newinfo.category" ng-options="inventory.category.name for inventory in info.objects"></select></td>
    <td><input type="text" ng-model="newinfo.status"></td>
    <td><button ng-click="createInventory()">Saglabāt</button></td>

【问题讨论】:

  • 不太清楚你在问什么。您可以通过探究问题来改进您的问题,直到您更确定您的问题是否与 AngularJS 或 Django 相关。并确保将您的帖子实际表述为一个问题:-)

标签: javascript python json django angularjs


【解决方案1】:

您必须指定一个类别。你得到一个错误,因为 django 需要类别的数据。您可以在 django 中指定default=null,因此您不能指定任何类别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多