Saifullah khan
Saifullah khan

Reputation: 778

how to serialize django.conf.settings to json

I am getting this error...

Object of type 'Settings' is not JSON serializable

Here is my code

from django.conf import settings
import json
def get_settings(request):
    responce = settings.__dict__
    return HttpResponse(json.dumps(responce),content_type='application/json')

Upvotes: 0

Views: 548

Answers (1)

heykarimoff
heykarimoff

Reputation: 739

django.conf.settings is not Json serializable, thought you can go throught and create dict() then give it to HttpResponse. Hope it helps!

import json
from django.http import HttpResponse
from django.conf import settings


def get_settings(request):
    context = {}
    for setting in dir(settings):
        if setting.isupper():
            context[setting] = getattr(settings, setting)

    return HttpResponse(json.dumps(context, indent=4), content_type="application/json")

Upvotes: 5

Related Questions