These are chat archives for django/django

16th
Jan 2018
Anish Shrestha
@annshress
Jan 16 2018 03:40
@SanskarSans Have a close look at the form being rendered!
:]
@elcolie
Jan 16 2018 04:40
@Demo318 :+1:
Prateek Dabas
@dprateek11
Jan 16 2018 05:37
def post_create(request):
form = PostForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
print form.cleaned_data.get('title')
instance.save()
context = {
    'form': form
}

form = PostForm(request.POST or None) .

what does this means

??
and also
instance = form.save(commit=False)
what is this line of code doing??
djangoReactGuy
@SanskarSans
Jan 16 2018 07:03

@dprateek11 Here it is

#create a form instance with POST data
form = PostForm(request.POST or None)

The first parameter of a Django form is for the forms data. If the operation of form was POSTed, it would have data and therefore create the instance of the form with those data so it can be further validated later on using the is_valid method.Its a bound form because you have passed the data. The form is bounded or not will be known by is_valid method. If the data is not provided, the form will not be validated by
is_valid and it will call is_bound method whose job is to primarily check if there is data or not.

form = PostForm()

This is unbound form

From the django docs

This save() method accepts an optional commit keyword argument, which accepts >>either True or False. If you call save() with commit=False, then it will return an object >>that hasn't yet been saved to the database.

In this case, it's up to you to call save() on the resulting model instance. This is useful if >>you want to do custom processing on the object before saving it, or if you want to use >>one of the specialized model saving options. commit is True by default.

I have to see, how to quote the text properly :smile:
Prateek Dabas
@dprateek11
Jan 16 2018 09:12
@SanskarSans Thank you :smile: