强制转换为Unicode:需要字符串或缓冲区,在Django表单上找到函数

时间:2016-07-26 16:04:32

标签: python django forms unicode

我是Django的新手,当我按下提交时我正在制作一个表单我收到的错误是我之前没见过的:TypeError at /catalog/ coercing to Unicode: need string or buffer, function found

我的forms.py看起来像是:

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'))

我的views.py看起来像是:

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')

错误发生在deploy(request, form)undeploy(request, form)上,它们在另一个应用上,我从app/views.py导入。

我在这里展示其中一个,因为我认为这两个问题都是一样的,但我无法修复它......

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')

错误在线:

deployApp = apacheStratosAPI + applications + '/' + applicationID + '/' + deploy + '/' + policy

当我调试时,我看到变量是正确的并且格式为u'value'。我不知道为什么现在我收到Unicode错误,因为在其他表单上我的值是这种格式,我没有收到任何错误。

为什么我现在收到此错误?谢谢和问候。

1 个答案:

答案 0 :(得分:1)

您在字符串连接中包含的内容之一是deploy。由于未在当前范围内定义,因此Python将查找该名称的最近声明,这是当前函数本身,因此是错误。

我不知道该名称实际上应该定义在哪里,但你应该在你的函数中做到这一点;并且,在任何情况下,您都需要重命名变量或函数。