为什么我们在 django 中这样写 form = StudentForm(request.POST)?
Why we write this, form = StudentForm(request.POST) in django?
这是我的观点功能,
def studentcreate(request):
reg = StudentForm()
string = "Give Information"
if request.method == "POST":
reg = StudentForm(request.POST)
string = "Not Currect Information"
if reg.is_valid():
reg.save()
return render('http://localhost:8000/accounts/login/')
context = {
'form':reg,
'string': string,
}
return render(request, 'student.html', context)
这里我们首先将表格存储在 reg 变量中然后我们也写 reg = StudentForm(request.POST) 为什么?
实际上我们为什么要写这个?
我不能告诉你为什么要写这篇文章。也许只有你自己知道。这没有多大意义。我建议阅读 https://docs.djangoproject.com/en/4.0/topics/forms/#the-view
上的 Django 文档
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
如果请求是 POST,则从数据中读取。否则,return 一个空表格。
您可以将“request.POST”视为传递到视图中的表单的参数。这告诉视图所提到的表单具有来自 name.html 中表单的 POST 数据。否则它只是一个空表格。
这是我的观点功能,
def studentcreate(request):
reg = StudentForm()
string = "Give Information"
if request.method == "POST":
reg = StudentForm(request.POST)
string = "Not Currect Information"
if reg.is_valid():
reg.save()
return render('http://localhost:8000/accounts/login/')
context = {
'form':reg,
'string': string,
}
return render(request, 'student.html', context)
这里我们首先将表格存储在 reg 变量中然后我们也写 reg = StudentForm(request.POST) 为什么? 实际上我们为什么要写这个?
我不能告诉你为什么要写这篇文章。也许只有你自己知道。这没有多大意义。我建议阅读 https://docs.djangoproject.com/en/4.0/topics/forms/#the-view
上的 Django 文档from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
如果请求是 POST,则从数据中读取。否则,return 一个空表格。
您可以将“request.POST”视为传递到视图中的表单的参数。这告诉视图所提到的表单具有来自 name.html 中表单的 POST 数据。否则它只是一个空表格。