How can I refactor the validation code to minimize it? I am using Django Rest Framework and below is the code of one of the API Endpoints. The code creates generates the PUBLIC and SECRET Values based on Stellar SDK.
def create(self, request):
        try:
            if 'app_id' not in request.data:
                response = {'status': 'FAIL', 'data': 'App ID field not found'}
                return Response(response, status=status.HTTP_400_BAD_REQUEST)

            if 'customer_id' not in request.data:
                response = {'status': 'FAIL', 'data': 'Customer ID field not found'}
                return Response(response, status=status.HTTP_400_BAD_REQUEST)

            customer_id = request.data['customer_id']
            app_id = request.data['app_id']
            app = App.objects.get(id=app_id)

            # Creating Stellar Account
            addr, secret = create_account()

            if addr is None and secret is None:
                response = {'status': 'FAIL', 'data': 'Could not generate customer Wallet'}
                return Response(response, status=status.HTTP_200_OK)

            # Make sure we do not add duplicate customers
            wallet_count = Wallet.objects.filter(customer_id=customer_id).count()
            if wallet_count &gt; 0:
                response = {'status': 'FAIL', 'data': 'The customer ID already exist'}
                return Response(response, status=status.HTTP_200_OK)

            # The customer does not exist, hence create his wallet.
            wallet = Wallet(customer_id=customer_id, app=app, address=addr, secret=secret)
            wallet.save()

            if wallet.pk is None and wallet.pk &gt; 0:
                response = {'status': 'OK', 'data': 'success'}
            else:
                response = {'status': 'FAIL', 'data': 'Messed up'}
        except App.DoesNotExist:
            response = {'status': 'FAIL', 'data': 'The App ID not found'}
            return Response(response, status=status.HTTP_404_NOT_FOUND)

        return Response({'status': 'OK', 'data': 'success'}, status=status.HTTP_200_OK)

As you can see there is repetition of codeblock like below:
response = {'status': 'FAIL', 'data': 'The customer ID already exist'}
                return Response(response, status=status.HTTP_200_OK)

It'd be nice if I can reduce all this code either by passing to a function or someway once and let it handle based on request dict.
Exception handling
You may want to consider reducing the try block to where the expected exception actually is raised.
try:
    app = App.objects.get(id=app_id)
except App.DoesNotExist:
    …

Possible bugs
This test does not consider the case, that only one of addr and secret is None. Is that permitted?
if addr is None and secret is None:
    …

These response dicts are never used:
if wallet.pk is None and wallet.pk &gt; 0:
    response = {'status': 'OK', 'data': 'success'}
else:
    response = {'status': 'FAIL', 'data': 'Messed up'}

Deduplication
Regarding your concern of duplicated dicts: I see none.
Sure, they are similar, but there are no duplicates, since the error messages differ. Of course, you can outsource the generation to a function like
def make_response(data: str, success: bool) -&gt; dict:
    &quot;&quot;&quot;Returns a response dict.&quot;&quot;&quot;
    return {'status': 'OK' if success else 'FAIL', 'data': data}

but it's up to you to decide if that's worth an extra function.