简体   繁体   中英

Django returning dictionary from front end?

I'm passing a dictionary from the front end to the back end in one of my Django views. The setup is something like this:

$.ajax({
        type: "POST",
        url: '/my/url',
        data: {
            'patients': '{{ appointments.appointments.items }}'
        },
        dataType: 'json',
                async: false,
        success: function (data) {
              console.log("yay")
        }
      });

which is passing back an dictionary of names, etc. and I'm then retrieving this on the python backend, like so:

if request.method == "POST":
    patients = request.POST.get("patients", None)
    print("patients are", patients)

However, when I do this and print out the patients dict() instead of getting an actual dictionary, I get a string like this:

dict_items([(153, {'person': 'Samuel', 'time': datetime.datetime(2021, 4, 22, 17, 0, tzinfo=<UTC>), 'number': 'First'})])

that doesn't follow the dictionary format/even really allow for json parsing etc.

Is there something I'm doing wrong here? The {{appointments.appointments.items}} is just passed into my HTML template page from the backend during the get request and I'm accessing it with JavaScript in the same file.

Any advice here would be appreciated.

I think, it can be solved by adding Eval Method.

patients = eval(request.POST.get("patients", None))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM