Django app fetch data from external API

I have deployed a django app and postgresDB using blueprint. It starts and works. The problem is when app must fetch information using this function, it returns error code status 500. In local environment it works perfect. Where is my mistake, please help me!

function getFoodVarieties(foodName) {
const csrftoken = document.querySelector(‘[name=csrfmiddlewaretoken]’).value;

        fetch('/api/get_food_varieties\/', {
            method: 'POST',
            body: JSON.stringify({food: foodName, index: document.getElementById('mealVariety').value}),
            headers: {'Content-Type': 'application/json', 'X-CSRFToken': csrftoken},
        }).then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        }).then(data => {
            var dropdown = document.getElementById('mealVariety');

            // Clear any existing options
            dropdown.innerHTML = '';

            // Populate dropdown with food varieties
            data.varieties.map(function (variety, index) {
                var option = document.createElement('option');
                option.text = variety;
                option.value = index;
                dropdown.add(option);
            });
        }).catch(error => {
            console.error('There was a problem with the fetch operation:', error);
        });
    }

    document.getElementById('mealName').addEventListener('change', function () {
        getFoodVarieties(this.value);
    });

    document.getElementById('id_quantity').addEventListener('change', function () {
        searchFood(this.value)
    });

Hi,

There will always be differences between environments: development mode/Local, production mode/Render, etc. These differences need to be considered and configured as required for your own app in each environment.

If your service is raising a 500, the best place to start would be the service logs.

However, we often see that Django’s default logging in production mode/Debug=False suppresses a lot of detail.

I’m not a Python/Django expert, and general code debugging is beyond the scope of our support, but based on this Stackoverflow post, maybe try adding more logging to the settings.py, temporarily to see if the error will appear in the logs, some like:

import sys

and a LOGGING declaration

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'DEBUG',
        },
        'MYAPP': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

Alan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.