Почему мы пишем это, form = StudentForm(request.POST) в 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) почему? действительно, почему мы пишем это?
Я не могу сказать вам, почему вы пишете это. Может быть, только вы знаете. Это не имеет особого смысла. Я бы рекомендовал прочитать документацию Django по этому вопросу на https://docs.djangoproject.com/en/4.0/topics/forms/#the-view
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. В противном случае возвращаете пустую форму.
Вы можете рассматривать "request.POST" как параметр, передаваемый форме в представлении. Это говорит представлению, что упомянутая форма имеет данные POST из формы в файле name.html. В противном случае это просто пустая форма.