Hi I am building an application in which my Ionic App need to send a post message to Django Backend.
My data at Ionic end during pos seem to be like this
// This is the value from ion-select with multiple set true
let data = {'selectedId':[1,2,3,4,5,6]}
In my django request.POST the data is converted like this
<QueryDict : {u'selectedId[]' : [u'1',u'2',u'3',u'4',u'5']}>
Why does the key value automatically converted from "selectedId" in Ionic to "selectedId[]"
if i try to get the array value in Django by performing
request.POST['selectedId[]'] this give me the length of the array 5
request.POST['selectedId'] this give me a MultiValueDictKeyError
The behavior you are seeing is because you are posting the values to you django endpoint as form-encoded values. When you post non-file values that are form-encoded, they are always posted as strings. If you want to preserve the type of the input you are passing in, you should use a JSON post instead of a form post.
Related
I use django-multiselectfield package in my project and based on its docs I use MultipleChoiceField in my serializer:
class InsCatSerializer(serializers.ModelSerializer):
levels = fields.MultipleChoiceField(choices=LEVEL)
when I send a request with raw JSON in postman that works fine
"levels": ["INTERMEDIATE", "ADVANCED"]
But I need to use form data because I have files and images in my request!
I try this way:
levels:INTERMEDIATE
levels:ADVANCED
but just saved the last element ( ADVANCED in this example )
any suggestion to solve?
Json Array and Form data can't work together. Please stringify your array or don't use form data at all.
Read more: JS, how to append array in FormData?
I'm making a system to track entries to a sports day event and I can get the data from the form to the Python back-end but I don't know how to get the data for the event entries to the back-end too.
I have a form I've already created using Flask and WTForms and I can submit all the data relating to the user but since they can enter from just a single event all the way up to every event they are able to enter the form will have a variable number of selection fields, I want to pack this data from the selection fields into a JSON string and then have Python process it since that is very easy. My only problem is, how can I get this data into a JSON string then send it in a single request to the back-end with the other data, like first name, last name etc.
Screenshot showing the user interface of the form
from flask import jsonify
#app.route('/selects')
def selects():
selects = ['one', 'two']
return jsonify(selects)
I have the same question and found answer in this tutorial. If you already use WTForm to make the form, the content can be access via methods. This should be straightforward. https://pythonspot.com/category/pro/web/page/2/
When I POST my form, I receive the following exception:
act_date: ["Date has wrong format. Use one of these formats instead: YYYY-MM-DD."]
And the same validation error for other DateFields.
I changed the default date format in extjs (Ext.util.Format.defaultDateFormat= 'Y-m-d') which did not work.
So next I define date format in Django setting:
'DATE_FORMATS': [("%Y-%m-%d"),],
This also hasn't worked.
When you said I changed the default date format in extjs (Ext.util.Format.defaultDateFormat= 'Y-m-d') which did not work. it's the date format don't work or again you'r server validation ?
Cause actually if i do this :
var d = new Date();
Ext.Date.format(d, 'Y-m-d');
That give me : "2019-03-05" and it's seem to be correct.
Have you check inside of you'r POST request the format of the sending date ?
You maybe have a unexpected date format before sending you'r request.
I have a huge list that will be generated dynamically from a csv file in a django view, but i had a requirement to use that list in the next view, so i thought to give a try on django sessions
def import_products(request):
if request.method == 'POST':
if request.FILES:
csv_file_data = ...........
total_records = [row for row in csv_file_data]
request.session['list_data'] = total_records
# total_records is `list of lists` of length more than 150
# do some processing with the list and render the page
return render_to_response('product/import_products.html')
def do_something_with_csv_data_from_above_view(request):
data = request.session['list_data']
# do some operations with list and render the page
return render_to_response('website/product/success.html',
So as mentioned in the above, i need to use the total_records huge list in the do_something_with_csv_data_from_above_view view and delete the session by storing it in another variable
So actually how to implement/use exactly the sessions concept(i have read the django docs but could n't able to get the exact concept)
In my case,
When a user tries to upload the csv file each time, i am reading the data and storing
the data as list to session
==> Is this the right way to do so ? also i want to store the session variable in
database concept
Because the list was huge, i need to delete it for sure in the next view when i
copied it in to another variable
Am i missing anything, can anyone please implement exact code for my above scenario ?
You have two options:
Client side RESTful - This is a RESTful solution but may require a bit more work. You can retrieve the data in the first request to the client and after selecting you can send the selected rows back to the server for processing, CSV etc.
Caching: In the first request you can cache your data on the server using a django file system or memcached. In the second request use the cache key (which would be the user session key + some timestamp + whatever else) to fetch the data and store in the db.
If it's a lot of data option 2 may be better.
am getting the following post data in my django app
POST
Variable Value
csrfmiddlewaretoken u'LHM3nkrrrrrrrrrrrrrrrrrrrrrrrrrdd'
id u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null'
am trying to get the id value in variable id i.e "id":4 (the value 4). When I do request.POST.get('id')I get the whole json string. u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null' How can I get the "id" in the string?
The data you are sending is simply a json string.
You have to parse that string before you can access data within it. For this you can use Python's json module (it should be available if you're using Python 2.7).
import json
data = json.loads( request.POST.get('id') )
id = data["id"]
If you somehow don't have the json module, you can get the simplejson module.
For more details, refer this question : best way to deal with JSON in django
That's happening because id is string, not dict as it should be. Please provide your template and view code to find source of problem.