Как получить значение поля выбора Django? - PullRequest
0 голосов
/ 13 мая 2019

Я не могу прочитать данные из поля выбора формы 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, но я отправил.

1 Ответ

0 голосов
/ 13 мая 2019
if form.is_valid():
                recipe_vote = form.data.get('vote')

используйте это, чтобы прочитать голосование

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...