2016-07-26 47 views
0

我是Django的新成員,我在創建表單時提交我收到以前未見過的此錯誤:TypeError at /catalog/ coercing to Unicode: need string or buffer, function found強制轉換爲Unicode:需要字符串或緩衝區,在Django表格上找到函數

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)和發生錯誤,他們在另一個應用程序,我從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

你在字符串連接中包含的東西之一是deploy。由於沒有在當前範圍內定義,因此Python將查找最近的該名稱聲明,該聲明是當前函數本身,因此是錯誤。

我不知道這個名字實際上應該在哪裏定義,但是你應該在你的函數中做;在任何情況下,您都需要重命名變量或函數。

+0

您的評論睜開了我的眼睛! 'deploy'變量是在'app/views.py'的頂層定義的,然後我將該函數的名稱更改爲'deploy',並忘記重命名該變量。我忘了Python無法區分它們。什麼愚蠢的錯誤。謝謝! – Aker666

相關問題