我想发布到我的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的新手,我不明白这个错误.有任何想法吗?
我的代码是
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) 我有一个 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)