【发布时间】:2017-01-25 19:25:51
【问题描述】:
基本上我希望用户在字段中输入一个数字并打印斐波那契结果,但是一旦将输入插入输入字段,我就会收到它返回 None 而不是对象
我的 html 表单在这里有这段代码:
{% block content %}
<form method="POST" action=".">{% csrf_token %}
<input type="text" name="fcal" value="{{F}}" />
<p>
<input type="submit" name="fibo" value="Fibonacci" />
</p>
</form>
{% if cal %}
Your Fibonacci answer is {{cal}}
{% endif %}
{% endblock %}
我views.py的内容是这个:
from django.shortcuts import render_to_response, get_object_or_404, render
from django.forms import ModelForm
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseNotFound
import fibonacci
def fibocal(request):
if request.method == 'POST':
fb = request.POST['fcal']
cal = fibonacci.fibo(fb)
return render(request, 'fibb/fibb.html', {'cal': cal})
else:
return render(request, 'fibb/fibb.html', {})
我的斐波那契函数是这个:
def fibo(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
result = fibo(n - 1) + fibo(n - 2)
return result
我想知道错误是在哪里引起的,因为我一遍又一遍地查看,我可以看到我返回的渲染不是 None
完整的追溯包括:
Traceback:
File "C:\python35\lib\site-packages\django\core\handlers\base.py" in get_response
158. % (callback.__module__, view_name))
Exception Type: ValueError at /fibb/
Exception Value: The view fibb.views.fibb didn't return an HttpResponse object. It returned None instead.
【问题讨论】:
-
你确定你的代码在调用渲染之前没有出错吗?你能编辑你的问题并发布完整的回溯吗?
-
只有当我在输入表单中输入值并按下提交按钮时才会出现错误
-
你可以为 fibb 视图添加代码吗..
-
修复缩进;错误很可能存在。
-
错误出现在
fibb视图中。但是您显示的是fibo。显示该视图