我正在尝试从Django表单获取单个输入,但它给了我错误 / books / f /中的 ValueError 视图main.views.select_team没有返回HttpResponse对象。而是返回None。
视图
onlyOwner
表格
Ownable.sol
url
def select_team(request):
if request.method == 'POST':
Team.objects.create(user=request.user, total=request.POST('team_number'))
return redirect(reverse('books', args=()))
答案 0 :(得分:2)
首先,您必须在击中url来测试表单时渲染模板。为此,请使用render。
return render(request, 'template_name.html')
然后使用request.POST.get('team_number')
来获取已发布的数据。因此,您现在的视图是:
def select_team(request):
if request.method == 'POST':
Team.objects.create(user=request.user, total=request.POST.get('team_number'))
return redirect(reverse('books', args=()))
return render(request, 'template_name.html')
而且您要双引输入的name="team_number
属性,因此模板应为:
<form action="{% url 'select_team' %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="number" id="start" name="team_number" min="2" max="5" value="2"/>
<label for="start">select in range of 2 to 5</label>
<button type="submit" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-remove"></span> create teams
</button>
</form>
答案 1 :(得分:1)
对于非POST请求,您的视图不返回任何内容。您应该这样向视图添加return语句:
def select_team(request):
if request.method == 'POST':
Team.objects.create(user=request.user, total=request.POST['team_number'])
return redirect(reverse('books', args=()))
return render(request, 'template_name.html')