Reputation: 540
I'm new on Django and I'm making a formwhen I press submit I'm getting this error that I haven't seen before: TypeError at /catalog/ coercing to Unicode: need string or buffer, function found
My forms.py
looks like:
class AppsForm(forms.Form):
def __init__(self, *args, **kwargs):
policiesList = kwargs.pop('policiesList', None)
applicationList = kwargs.pop('applicationList', None)
super(AppsForm, self).__init__(*args, **kwargs)
if policiesList and applicationList:
self.fields['appsPolicyId'] = forms.ChoiceField(label='Application Policy', choices=policiesList)
self.fields['appsId'] = forms.ChoiceField(label='Application', choices=applicationList)
else:
self.fields['appsPolicyId'] = forms.ChoiceField(label='Application Policy',
choices=('No application policies found',
'No application policies found'))
self.fields['appsId'] = forms.ChoiceField(label='Application', choices=('No applications found',
'No applications found'))
My views.py
looks like:
def main(request):
if validateToken(request):
appList = getDetailsApplications(request)
polList = getDetailsApplicationPolicies(request)
message = None
if request.method == 'POST' and 'deployButton' in request.POST:
form = AppsForm(request.POST, policiesList=polList, applicationList=appList)
if form.is_valid():
deploy(request, form)
else:
form = AppsForm(policiesList=polList, applicationList=appList)
message = 'Form not valid, please try again.'
elif request.method == 'POST' and 'undeployButton' in request.POST:
form = AppsForm(request.POST, policiesList=polList, applicationList=appList)
if form.is_valid():
undeploy(request, form)
else:
form = AppsForm(policiesList=polList, applicationList=appList)
message = 'Form not valid, please try again.'
else:
form = AppsForm(policiesList=polList, applicationList=appList)
return render_to_response('catalog/catalog.html', {'message': message, 'form': form},
context_instance=RequestContext(request))
else:
return render_to_response('menu/access_error.html')
The error happens on deploy(request, form)
and undeploy(request, form)
, they are on another app and I import both from the app/views.py
.
Here I show one of them because I think the problem on both it's the same, but I'm not unable to fix it...
def deploy(request, form):
if validateToken(request):
policy = form.cleaned_data['appsPolicyId']
applicationID = form.cleaned_data['appsId']
headers = {'Content-Type': 'application/json'}
deployApp = apacheStratosAPI + applications + '/' + applicationID + '/' + deploy + '/' + policy
req = requests.post(deployApp, headers=headers, auth=HTTPBasicAuth(request.session['stratosUser'],
request.session['stratosPass']),
verify=False)
if req.status_code == 202:
serverInfo = json.loads(req.content)
message = '(Code: ' + str(req.status_code) + ') ' + serverInfo['message'] + '.'
return render_to_response('catalog/catalog.html', {'message': message, 'form': form},
context_instance=RequestContext(request))
elif req.status_code == 400 or req.status_code == 409 or req.status_code == 500:
serverInfo = json.loads(req.content)
message = '(Error: ' + str(req.status_code) + ') ' + serverInfo['message'] + '.'
return render_to_response('catalog/catalog.html', {'message': message, 'form': form},
context_instance=RequestContext(request))
else:
return render_to_response('menu/access_error.html')
The error it's on the line:
deployApp = apacheStratosAPI + applications + '/' + applicationID + '/' + deploy + '/' + policy
When I debug, I see that the variables are correct and on format u'value'
. I don't know why now I'm getting the Unicode error because on other forms that I have the values are on this format and I don't get any error.
Why I get this error now? Thanks and regards.
Upvotes: 0
Views: 482
Reputation: 599778
One of the things you're including in that string concatenation is deploy
. Since that isn't defined within the current scope, Python will look for the nearest declaration of that name, which is the current function itself, hence the error.
I don't know where that name is actually supposed to be defined, but you should probably do it within your function; and, in any case, you will need to rename either the variable or the function.
Upvotes: 1