簡體   English   中英

Django從視圖函數中調用另一個函數

[英]Django calling an another function inside from a view function

views.py這樣的東西

def getFormValues(request):
if ('mobile_a' in request.GET and request.GET['mobile_a']) and ('mobile_b' in request.GET and request.GET['mobile_b']):
    mobile_a = request.GET['mobile_a']
    mobile_b =request.GET['mobile_b']
    # calling the mark calculation function
    return calculateMark(mobile_a, mobile_b)
else:
    message ='You submitted an empty form.'
    return HttpResponse(message)

在這里,我正在調用一個函數(calculateMark)來計算標記。 在calculateMark函數的最后,我有這樣的內容

if (tech_mark_a == 0 and tech_mark_b == 0):
    mobile_a_infos = TechSpecificationAdd.objects.filter(tech_variables)
    return render_to_response('degrees_result.html', {'mobile_a': tech_mark_a, 'mobile_b': mobile_b}, mobile_a_infos)

elif (tech_mark_a > 0 and tech_mark_b > 0):
    return render_to_response('degrees_result.html', {'mobile_a': tech_mark_a, 'mobile_b': mobile_b})

問題是當我提交一個空表單時,它將消息顯示為getFormValues()。 但是,當我在表單上提交某些內容時,它會顯示錯誤。視圖MarkCalculation.views.getFormValues沒有返回HttpResponse對象。 我該如何解決這個問題? 提前致謝。

修復您的calculateMark()函數,使其不包含任何不返回響應的代碼路徑。 另外,請考慮先將參數轉換為數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM