小编Gro*_* Ni的帖子

发布到django休息框架

我想发布到我的Django服务器,post所以我可以添加一个todo项目.这是模型:

class Todo(models.Model):
    title = models.CharField(max_length=200);
    text = models.TextField()
    completed = models.BooleanField(default=False)
    created_at = models.DateTimeField(default=datetime.now, blank = True )
    def __str__(self):
        return self.title
Run Code Online (Sandbox Code Playgroud)

和序列化器:

class TodoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Todo
        fields = ("id", 'title','text', 'completed', 'created_at')
Run Code Online (Sandbox Code Playgroud)

并查看:

class TodoList(APIView):
    def get(self,request):
        todo=Todo.objects.all()
        serializer=TodoSerializer(todo,many=True)
        return Response(serializer.data)
    def post(self,request):
        Todo.objects.create(
            title=request.POST.get('title'),
            text=request.POST.get('text'))
        return HttpResponse(status=201)
Run Code Online (Sandbox Code Playgroud)

我的帖子请求是

{ "title": "new title",
  "text": "a test text"}
Run Code Online (Sandbox Code Playgroud)

它告诉我

IntegrityError at /todos/
(1048, "Column 'title' cannot be null")
Run Code Online (Sandbox Code Playgroud)

作为Django的新手,我不明白这个错误.有任何想法吗?

python django post django-rest-framework

5
推荐指数
2
解决办法
2万
查看次数

Ruby的数组如何工作?

当我输入

p [[2,1],3,4][1][1]
Run Code Online (Sandbox Code Playgroud)

它将输出1.

为什么会这样?

ruby arrays

4
推荐指数
1
解决办法
85
查看次数

导入错误:没有名为 SpeechRecognition 的模块

我的代码是

import SpeechRecognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# recognize speech using Microsoft Bing Voice Recognition
BING_KEY = "Somevalue"  # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings
try:
    print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
except sr.UnknownValueError:
    print("Microsoft Bing Voice Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Microsoft Bing Voice Recognition …
Run Code Online (Sandbox Code Playgroud)

python speech-recognition pip

4
推荐指数
1
解决办法
8074
查看次数

布尔数组中的随机位置

我有一个 2D 布尔数组,我想生成一个随机位置1

array([[0, 0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 1, 1, 1, 1, 1],
       [0, 0, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0, 0, 0, 1],
       [0, 1, 1, 1, 0, 0, 1, 1]])
Run Code Online (Sandbox Code Playgroud)

输出示例:

(2,2)
Run Code Online (Sandbox Code Playgroud)

python random numpy

-1
推荐指数
1
解决办法
78
查看次数