我正在通过模板表单的POST请求,将视图中的两个参数传递给表单的 init 方法。但是它总是有一个GET方法。 这是我的代码:
views.py
form = formA(team_one=team_one, team_two=team_two)
print(request.method)
if request.method == 'POST':
team_one = match_obj.first().short_team_one
team_two = match_obj.first().short_team_two
forms.py
class formA(forms.Form):
team_one = ""
team_two = ""
def __init__(self, *args, **kwargs):
team_one = kwargs.pop("team_one")
team_two = kwargs.pop("team_two")
super(formA, self).__init__(*args, **kwargs)
obj = Detail.objects.filter(Q(current_one=team_one) | Q(current_two=team_two),)
for o in obj:
self.fields[o.name] = forms.BooleanField()
模板
<form method="POST" action="">
{% csrf_token %}
<input class="btn btn-success btn-sm" type="submit" name="submit" id="signup" value="Create" style="float:right; right:20px;">
</form>
请帮助!