javascript - AttributeError: 'unicode' object has no attribute 'get' - In Django Forms -
i'm trying use django forms ajax calls.
previously used html form information through request.post['item']. i've been thinking validators, , benefit if switched normal html forms django forms.
in html code (the page user clicks, , ajax calls view javascript):
if not request.user.is_authenticated(): #tells user login if not authenticated return redirect('/webapp/login.html') else: #get logger logger = logging.getlogger('views.logger.chartconfigure') logger_uuid = uuid.uuid4() logger_time = datetime.datetime.now() #log user logger.info("request in editchart, user:" + str(request.user.username) + ", uuid:" + str(logger_uuid) + ", time:" + str(logger_time)) #forms use chartname = changechartnameform(auto_id=false) #put forms context context = {'chartnameform': chartname} #return context return render(request, 'webapp/editchart.html', context)
the forms used changechartnameform:
#form editing chart names class changechartnameform(forms.form): #only 1 variable called chartname, label set "" #since don't want labels. have own in html. chartname = forms.charfield(max_length=100, label="") #form-control class required bootstrap 3, , html id #of form called chartname chartname.widget.attrs['class'] = 'form-control' chartname.widget.attrs['id'] = 'chartname'
html code:
<div class="input-group"> <span class="input-group-btn"> <button class="btn btn-default" type="button" id="newchartname" >new chart name</button> </span> {{ chartnameform }} </div>
the javascript code:
$.ajax( { type:"post", url:"ajax_postcolumnaction/", datatype: 'json', data: { 'csrfmiddlewaretoken':csrftoken, 'currenttabselected':currenttabselected, 'currentcolumnselected':currentcolumnselected, 'action':'changename', 'changenameform':$('#chartname').serialize() }, success: function(response) { ...some logic happens here } }
basically javascript code call view, called ajax_postcolumnaction:
#get name form, , newname changenameform = changechartnameform(request.post['changenameform']) newname = "" if(changenameform.is_valid()): newname = changenameform.cleaned_data['chartname']
the return always:
'unicode' object not have attribute 'get' @ following line: if(changenameform.is_valid())
i have tried following:
- using data=request.post
- using data=request.post['changenameform']
full traceback:
traceback (most recent call last): file "c:\users\desktop\dropbox (personal)\django\dashboard_web\webapp\views.py", line 738, in ajax_postcolumnaction if(changenameform.is_valid()): file "c:\python27\lib\site-packages\django\forms\forms.py", line 129, in is_valid return self.is_bound , not bool(self.errors) file "c:\python27\lib\site-packages\django\forms\forms.py", line 121, in errors self.full_clean() file "c:\python27\lib\site-packages\django\forms\forms.py", line 273, in full_clean self._clean_fields() file "c:\python27\lib\site-packages\django\forms\forms.py", line 282, in _clean_fields value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) file "c:\python27\lib\site-packages\django\forms\widgets.py", line 207, in value_from_datadict return data.get(name, none) attributeerror: 'unicode' object has no attribute 'get'
edit:
when do:
print request.post['changenameform']
i chartname = "some text typed in browser"
this part of error says data
unicode string:
return data.get(name, none) attributeerror: 'unicode' object has no attribute 'get'
data
needs object. instead, string, , strings don't have get()
method, , don't have name
attributes error trace says.
try going off of django docs call ajax:
https://docs.djangoproject.com/en/1.6/topics/class-based-views/generic-editing/#ajax-example
Comments
Post a Comment