我在将表单字段保存到数据库时遇到问题。我知道它不能保存,因为如果我在django中查看Player模型,总会有0个数据。如果有人可以看一看并纠正我,我会非常感激。
models.py -
template <class T>
struct Task
{
template <typename ...Args>
void operator()(const Args&... args)
{
(*static_cast<const T*>(this))(args...);
}
};
forms.py -
from django.db import models
class Player(models.Model):
player_one_name = models.CharField(max_length=30, default='')
player_two_name = models.CharField(max_length=30, default='')
views.py
from django import forms
class PlayerInfo(forms.Form):
player_one_name = forms.CharField(max_length=30, label='First player name')
player_two_name = forms.CharField(max_length=30, label='Second player name')
start.html - 提交每个玩家的名字
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, render_to_response
import os
from .forms import PlayerInfo
from .models import Player
def start(request):
if request.method == 'POST':
form = PlayerInfo(request.POST)
if form.is_valid():
obj = Player()
obj.player_one_name = form.cleaned_data['player_one_name']
obj.player_two_name = form.cleaned_data['player_two_name']
return HttpResponseRedirect('game/')
else:
form = PlayerInfo()
args = {'form': form}
return render(request, 'start.html', args)
game.html - 能够呈现每个玩家的名字
{% block botRow %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
答案 0 :(得分:1)
请求方法总是全部大写。
if request.method == 'POST':