예제 #1
0
def test_invalid_blog_post_serializer(client):

    invalid_serializer_data = {"tags": [], "body": "testbody"}
    serializer = BlogPostSerializer(data=invalid_serializer_data)
    assert not serializer.is_valid()
    assert serializer.validated_data == {}
    assert serializer.data == invalid_serializer_data
    assert serializer.errors == {"title": ["This field is required."]}

    invalid_serializer_data = {"tags": [], "title": "testtitle"}
    serializer = BlogPostSerializer(data=invalid_serializer_data)
    assert not serializer.is_valid()
    assert serializer.validated_data == {}
    assert serializer.data == invalid_serializer_data
    assert serializer.errors == {"body": ["This field is required."]}
예제 #2
0
def test_valid_blog_post_serializer(client):

    valid_serializer_data = {
        "tags": [],
        "title": "testtitle",
        "body": "testbody"
    }
    serializer = BlogPostSerializer(data=valid_serializer_data)
    assert serializer.is_valid()
    assert serializer.validated_data == valid_serializer_data
    assert serializer.errors == {}
예제 #3
0
def blogpost_list(request):
		print request.POST
		if request.method == 'GET':
			blogs = BlogPost.objects.all()
			serializer = BlogPostSerializer(blogs,many=True)
			return JSONResponse(serializer.data)
		elif request.method == 'POST':
			print "接收到post请求"
			# print request
			# data = JSONParser().parse(request.POST)
			print "成功"
			serializer = BlogPostSerializer(data = request.POST)
			print '系列化成功'
			# print serializer
			if serializer.is_valid():
				serializer.save()
				return JSONResponse(serializer.data,status=201)
			else:
				return JSONResponse(serializer.errors,status = 400)
예제 #4
0
def blogpost_detail(request,pk):
	print pk
	try:
		blogpost = BlogPost.objects.get(pk = pk)
	except BlogPost.DoesNotExist:
		# print JSONResponse("name":"no")
		return HttpResponse("没有这个ID")
	
	if request.method == 'GET':
		serializer = BlogPostSerializer(blogpost)
		return JSONResponse(serializer.data)
	elif request.method == 'PUT':
		data = JSONParser().parse(request)
		serializer = BlogPostSerializer(blogpost,data=data)
		if serializer.is_valid():
			serializer.save()
			return JSONResponse(serializer.data)
		else:
			return JSONResponse(serializer.errors,status=400)
	elif request.method == 'DELETE':
		blogpost.delete()
		return HttpResponse(status=204)