我无法从Django表单选择字段中读取数据。表单中的数据不会转到views.py或Forms.py中的表单。错误列表=选择一个有效的选择。汇率不是可用的选择之一。如何解决?
#views.py
def recipe_detail(request, recipe_name):
recipe = Recipe.objects.get(recipe_name=recipe_name)
form = VoteSubmissionForm()
if request.method == 'POST':
recipe = Recipe.objects.get(recipe_name=recipe_name)
if 'Like' in request.POST:
recipe_like = recipe.recipe_like
recipe_like = int(recipe_like) + 1
recipe = Recipe.objects.filter(recipe_name=recipe_name).update(recipe_like=recipe_like)
recipe = Recipe.objects.get(recipe_name=recipe_name)
return render(request, 'detail.html', {
'recipe': recipe,
'form': form,
})
elif 'vote' in request.POST:
form = VoteSubmissionForm(request.POST) #forma veri gelmiyor ?
if form.is_valid():
recipe_vote = recipe.recipe_vote
recipe_vote_count = recipe.recipe_vote_count
recipe_vote = (int(recipe_vote) * int(recipe_vote_count) + form.cleaned_data['vote']) / (int(recipe_vote_count) + 1)
recipe_vote_count = int(recipe_vote_count) + 1
recipe = Recipe.objects.filter(recipe_name=recipe_name).update(recipe_vote=recipe_vote, recipe_vote_count=recipe_vote_count)
recipe = Recipe.objects.get(recipe_name=recipe_name)
return render(request, 'detail.html', {
'recipe': recipe,
'form': form,
})
else:
return redirect('index')
return render(request, 'detail.html', {
'recipe': recipe,
'form': form,
})
#forms.py
VOTE_CHOICES = (
('0', '0'),
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10')
)
class VoteSubmissionForm(forms.Form):
vote = forms.ChoiceField(choices=VOTE_CHOICES, widget=forms.Select, label='Vote')
<form method="POST">
{% csrf_token %}
{{ form }}
<input class="btn btn-primary" type="submit" name="vote" value="Rate">
</form>
表格不是is_valid,但我确实提交了。
答案 0 :(得分:0)
if form.is_valid():
recipe_vote = form.data.get('vote')
使用它来阅读投票