2016-11-10 69 views
0

我有一個Django項目,並在其中一個網頁上,我想收集一些項目的信息。Python Django獲取或發佈?

一個字段有一個下拉列表,從中用戶可以選擇參與該項目的團隊成員的名字。此下拉列表中有許多預先定義的選項,並且還提供了選擇「其他」的選項。

如果用戶選擇「其他」,領域的變化「列表下拉」到「文本框」,用戶可以在他們想要選擇的人的名字輸入。在他們開始輸入時,數據庫將根據他們迄今輸入的字母來查詢,並在文本框下方顯示可用選項列表(與輸入的字母匹配)。例如,如果用戶鍵入了「D」,那麼將顯示的可用選項列表可能包括:'Dan','Dave','Debbie',但是如果他們鍵入了「Da」,則可用選項列表那將顯示將只包括:'丹'&'戴夫'。

當用戶第一次在項目中加載此頁面時,'name'字段爲空。當我開始在這一領域的打字,然後從所顯示的可用選項列表中的選項,瀏覽器控制檯顯示我該說的錯誤:

jquery-2.2.2.min.js:4 POST http://localhost:8000/projects/5915/submit_3_5_ajax/ 500 (Internal Server Error) 

我認爲,這可能意味着我使用錯誤的方法,但我同時使用Get & Post已經嘗試過,他們都給予在瀏覽器控制檯同樣的錯誤......

蟒蛇控制檯還顯示了這個錯誤:

ValueError: invalid literal for int() with base 10: '' 
[10/Nov/2016 14:05:38] "POST /projects/5915/submit_3_5_ajax/ HTTP/1.1" 500 21537 

爲f AR是我所知道的,異常的地方發生的窗體類裏面我forms.py文件:

class InfoForm(ValidatedForm): 
    ... 
    def __init__(self, *args, **kwargs): 
     ... 
     try: 
      who_will_organise = project.assigned.get(role=Role.O).employee.first_name + project.assigned.get(role=Role.O).employee.surname[0] # I just want to get the first character of the surname here... 
     except ObjectDoesNotExist: who_will_organise = None 

     ... 

    def save(self, commit=True): 
     ... 
     if data['who_will_organise']: 
      ... 
     else: 
      # The print statements I'm seeing in the console would indicate that this is where the exception is being thrown... 
      who_will_organise = Employee.objects.get(id=data['who_will_organise']) 
      try: 
       ... 
      except ... 
      ... 
     ... 
     return ... 

這個頁面的網址是:

url(r'^(?P<project_id>\d+)/survey/$', views.survey, name='survey'), 

view,它的調用與定義:

def survey(request, project_id): 
    project = Project.objects.get(id=project_id) 
    survey = Survey.objects.get_or_create(project=project)[0] 

    context = { 
     'project': project, 
     'survey_form': SurveyInformationForm(instance=survey), 
    } 
    return render(request, 'projects/survey.html', context) 

我的瀏覽器控制檯顯示以下錯誤消息:

POST http://localhost:8000/projects/5915/submit_3_5_ajax/ 500 (Internal Server Error) 

autosave_form.js:122 FAIL:(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i… 

FAIL:(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i… 

在Python控制檯在我的命令行,在那裏我運行Python的服務器,我得到錯誤信息:

OperationalError: (1054, "Unknown column 'costing_addomit.group_id' in 'field list'") 
[10/Nov/2016 15:01:24] "GET /projects/pipeline/ HTTP/1.1" 500 337434 

任何人都可以指出我,我做錯了什麼嗎?

+2

不符合您所展示的細節;目前還不清楚爲什麼你認爲這與獲得和發佈有關。你需要展示你的網址。py,view,以及瀏覽器開發工具網絡選項卡中顯示的完整錯誤。 –

+0

道歉 - 我已經添加了網址和查看問題。 – someone2088

+0

您發佈的網址中不包含「.../survey/...」... – Javier

回答

-1

它看起來像你正在鑄造一個字符串作爲一個Int的地方。

前一段時間我發生了類似的事情。堆棧跟蹤和/或打印這裏你最好的朋友。 我最好的猜測是上面的代碼有效,問題實際上是在其他地方。

檢查您要轉換爲int的位置,或期望null不爲null。問題可能在那裏。

+0

蛇皮控制檯顯示'ValueError':'文件 「/.../site-packages/django/db/models/fields/__init__.py」,線路976,在get_prep_value 返回INT(值) ValueError異常:無效的文字爲int()與基10:'' [10/Nov/2016 15:56:04]「POST/projects/5915/submit_3_5_ajax/HTTP/1.1」500 21537',我猜可能是你談論?在我看過的任何代碼(urls.py,views.py,&forms.py)中找不到'return int(value)'這一行,也無法在任何代碼中找到它我剛剛找到的'ajax.js'文件... – someone2088