Javascript 如何在Django中的页面上保存多个表单
我正在尝试在Django中制作一个博彩应用程序。用户登录后,会出现一个包含多个表单的页面,每个表单负责保存匹配结果。每个表单接受两个整数输入(每个团队的目标数)。我希望在这些表单的末尾有一个保存按钮,这样当单击时,输入数据会记录在数据库中。我有两个模型,游戏和打赌。Game负责存储游戏的实际结果,而Bet负责记录用户的预测Javascript 如何在Django中的页面上保存多个表单,javascript,django,forms,Javascript,Django,Forms,我正在尝试在Django中制作一个博彩应用程序。用户登录后,会出现一个包含多个表单的页面,每个表单负责保存匹配结果。每个表单接受两个整数输入(每个团队的目标数)。我希望在这些表单的末尾有一个保存按钮,这样当单击时,输入数据会记录在数据库中。我有两个模型,游戏和打赌。Game负责存储游戏的实际结果,而Bet负责记录用户的预测 class Game(models.Model): team1_name = models.CharField(max_length=100) team2_n
class Game(models.Model):
team1_name = models.CharField(max_length=100)
team2_name = models.CharField(max_length=100)
team1_score = models.IntegerField()
team2_score = models.IntegerField()
class Bet(models.Model):
user = models.ForeignKey(User)
game = models.ForeignKey(Game)
team1_score = models.IntegerField()
team2_score = models.IntegerField()
这是主页
{%用于游戏中的游戏%}
{%csrf_令牌%}
{{game.team1_name}
{{form.team1_score}
{{game.team2_name}
{{form.team2_score}
{%endfor%}
有关如何在Django上用一个表单处理多个重复字段的完整答案,请阅读此答案历史记录。这将是关于在Django中从一个页面处理多个表单的良好实践的答案。
因此,有一个配方,我们需要的是:
from django.shortcuts import render, get_object_or_404
from django.views import View
from django.http import HttpResponseBadRequest
class PlaceBet(View):
template_name = 'place_bets.html'
context = some_base_context_dict
def get(self, request):
# the user is requesting the game list
self.context['games'] = Game.objects.all()
return render(request, self.template_name, self.context)
def post(self, request, game_id=None):
# the user is submitting one of game's predictions
if not game_id:
return HttpResponseBadRequest('hey, send a game id, you fool!')
game = get_object_or_404(Game, pk=game_id)
# here you can use a Form to validate the data, but hey,
# do your homework
bet = Bet.objects.create(user=request.user, game=game,
team1_score=request.POST.get('team1_score'),
team2_score=request.POST.get('team2_score'))
# bet is saved at this point so now we can now take the user
# elsewhere or i dunno, the same page...
self.context['games'] = Game.objects.all()
self.context['new_bet'] = bet
response = render(request, self.template_name, self.context)
# it's good practice to say you created something
response.status_code = 201
return response
现在,URL也需要一些工作,您正在传递一个参数,所以
urlpatterns = [
url(r'^place_bet$', PlaceBet.as_view()),
url(r'^place_bet/(?P<game_id>[^/.]+)', PlaceBet.as_view(), name='place-bet') #name parameter, very important for the future...
]
urlpatterns=[
url(r“^place\u bet$”,PlaceBet.as\u view()),
url(r'^place\u bet/(?P[^/]+),PlaceBet.as_view(),name='place-bet')#name参数,对未来非常重要。。。
]
您的模板几乎正确,但需要做一些工作:
{% for game in games %}
<!-- the action now points to the URL by name, and passes the game_id -->
<form action="{% url 'place-bet' game.pk %}" method="POST">
{% csrf_token %}
<table>
<tr>
<th class="table-col">
<label for="team1_score">{{ game.team1_name}}</label>
</th>
<th class="table-col">
<!-- you need something to submit -->
<input type="number" name="team1_score" value="{{ game.team1_score }}">
</th>
</tr>
<tr>
<td class="table-col">
<label for="team2_score">{{ game.team2_name}}</label>
</td>
<td class="table-col">
<input type="number" name="team2_score" value="{{ game.team2_score }}">
</td>
</tr>
</table>
<input type="submit" value="Submit" id="submit-button"/>
</form>
{% endfor %}
{%用于游戏中的游戏%}
{%csrf_令牌%}
{{game.team1_name}
{{game.team2_name}
{%endfor%}
现在,当您按下提交按钮时,浏览器将使用编码游戏ID的动作URL将POST数据发送到视图的POST
方法,因此没有出错的余地
这段代码可以改进很多,但它会让您继续使用。听起来您想要的是一个包含更多字段的表单,而不是多个表单。@CarsonCrane我也考虑过这一点,但您不认为当预测的游戏数量越来越多时,事情会失控吗?谢谢您的全面回答,你能详细说明一下好的做法吗?干杯你知道,可能是因为我看错了问题,你在每个循环中重复提交按钮,所以每次提交一对字段。我会修正我的答案!