我正在从cs50 web编程Edx学习python并在表单添加任务上工作。如果表单有效,代码将添加任务,如果表单无效,则返回到(服务器端验证)的页面。
我正在使用Python3.
我代码:
# created two form inputs
class NewTaskForm(forms.Form):
task = forms.CharField(label="Task/kaam")
priority = forms.IntegerField(label="Priority", min_value=1, max_value="8")
# to show the added tasks
def index(request):
return render(request, "tasks/index.html", {'tasks': tasks})
# to add the task
def add(request):
if request.method == "POST":
form = NewTaskForm(request.POST)
if form.is_valid():
task = form.cleaned_data["task"]
tasks.append(task)
else:
return render(request, "tasks/add.html", {"form": form})
return render(request, "tasks/add.html",
{"form": NewTaskForm()}
)
您在这里使用了字符串:
# created two form inputs
class NewTaskForm(forms.Form):
task = forms.CharField(label="Task/kaam")
priority = forms.IntegerField(label="Priority", min_value=1, max_value="8") #<--- max_value="8"
试题:
# created two form inputs
class NewTaskForm(forms.Form):
task = forms.CharField(label="Task/kaam")
priority = forms.IntegerField(label="Priority", min_value=1, max_value=8)
# to show the added tasks
def index(request):
return render(request, "tasks/index.html", {'tasks': tasks})
# to add the task
def add(request):
if request.method == "POST":
form = NewTaskForm(request.POST)
if form.is_valid():
task = form.cleaned_data["task"]
tasks.append(task)
else:
return render(request, "tasks/add.html", {"form": form})
return render(request, "tasks/add.html",
{"form": NewTaskForm()}
)