无法从django选择表单中获取数据

时间:2017-12-11 01:26:46

标签: django django-forms

嗨,我正在尝试开发一个简单的django应用程序,但我无法访问表单的数据。我已经广泛地查看了django的文档,并且在这里有很多关于同一主题的问题,但没有任何工作。这是我的视图中的代码,否则正在工作:

def post(self, request):
    """Return only the games from the upcoming gameweek"""
    form = GWForm(request.POST)
    if form.is_valid():
        curr_gw = form.cleaned_data['gweek']
        args = {'form': form, 'this_gw_fixtures': Game.objects.filter(gweek=curr_gw), 'curr_gw': curr_gw}
        return render(request, self.template_name, args)
    else:
        curr_gw = 17
        form = GWForm()
        args = {'form': form, 'this_gw_fixtures': Game.objects.filter(gweek=curr_gw), 'curr_gw': curr_gw}
        return render(request, self.template_name, args)

这是我模板的代码:

<form action="/predict/" method="post">{% csrf_token %}
    <label for="Gameweek">Gameweek: </label>
    <input id="gwparam" type="number" value="{{ curr_gw }}" min="17" max="40">
    <input type="submit" value="Go">
</form>
    {% if this_gw_fixtures %}
        <ul>
        {% for game in this_gw_fixtures %}
            <li><a href="{% url 'predict:detail' game.id %}">{{ game }}</a></li>
        {% endfor %}
        </ul>


    {% else %}
        <p>No game predictions are available for this gameweek.</p>
    {% endif %}

我要做的是获取选择表单的输入并呈现在表单中选择的游戏周中的游戏列表。最小17,最大40.这是我的表格代码。

class GWForm(forms.Form):
    gweek = forms.ChoiceField(required = False, choices=[(x, x) for x in range(17, 40)])

但是当我尝试从表单中获取gweek时,is_valid()返回true,但是form.cleaned_data ['gweek']根本不返回任何值。任何帮助,将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

forms.py

Use Project TestNg jar.

模板

class GWForm(forms.Form):
    gweek = forms.IntegerField(required=False, min_value=17, max_value=40)