3

I'm starting to learn django and started watching tutorials on how to create forms and i've seen a lot of places where the form is created like this.

def create(request): if request.POST: form = ArticleForm(request.POST) if form.is_valid: form.save() else: form = ArticleForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('create_article.html', args) 

Now, assuming that I created a model called Article and then created an ArticleForm from that model, what exactly is going on here (in the code I provided above)? I understand the if form.is_valid: form.save() part, and according to what I read, request should always be the first parameter, but can someone explain what request as a parameter does and what the first two lines of the function are doing? And what exactly is going on in the else statement and after the else statement (the args part)?

EDIT: Also, suppose the Article model has a field called name = models.CharField(max_length=20), is there a way for me to get / access what the user entered for that particular section of the form? Suppose I want to get the name and see if the name already exists in my database, would there be a way for me to do that?

3 Answers 3

3

request.POST among other things (like the CSRF token value) contains all the data the user has entered in the form.

if request.POST 

checks if the user actually validated the form, otherwise there is no POST data in the request.

form = ArticleForm(request.POST) 

looks strange at first but when the user validates the form, the same page is loaded but the POST data is processed in the django form for data validation (like checking if a required field was left blank, etc…) in order to display errors in the form. If there is no error (form.is_valid()) then the view program continues.

Sign up to request clarification or add additional context in comments.

3 Comments

Hm okay, so suppose the Article model has a field called name = models.CharField(max_length=20), is there a way for me to get / access what the user entered for that particular section of the form? Suppose I want to get the name and see if the name already exists in my database, would there be a way for me to do that?
Absolutely yes, in place of "form.save()", you can get each field data (cleaned by the form btw) and process them individually without obligation to save them in the database. I'm not with my django development computer, I forgot the exact syntax to do it…
It might be « particularFieldName = form.cleaned_data['particularFieldName'] » for example.
2

I hope you are familiar with HTTP methods like GET and POST.

request object represents a single request by any user agent. So it can be a request that's sent from browser from you when you browse a particular page or from a crawler from a search engine. Read more about request here

request.POST is an attribute of this request object, it's a QueryDict (much similar to a normal Python dict). It contains the HTTP POST parameters that are sent to your view.

In short in your example:

def create(request): if request.POST: # check if the request is POST request and it contains any parameter form = ArticleForm(request.POST) # then pass all those parameters to the form if form.is_valid: # process the form to check if it's valid form.save() # save the data if it's valid else: form = ArticleForm() # if not valid data, initialize an new / empty form args = {} # create a dict to pass to the template args.update(csrf(request)) # add the CSRF token args['form'] = form # add the 'form' above to the 'args' dict return render_to_response('create_article.html', args) # pass that dict to template 

Not so sure why you have this example, normally I would do the last part like this:

def create(request): .... your code .... else: form = ArticleForm() return render(request, 'create_article.html', { form: form }) 

Hope it helps.

2 Comments

Yea okay perfect, I just realized that what you did is a lot better.. okay so the last question would be, suppose the Article model has a field called name = models.CharField(max_length=20), is there a way for me to get / access what the user entered for that particular section of the form? Suppose I want to get the name and see if the name already exists in my database, how would I go about doing so?
Yes you can get earlier in your view by using request.POST.get('name', '') or check it in your form, use the form is cleaner.
1

There are some mistakes in the code and it seems it's copy-pasted from a SO question. I would recommend going through the excellent Django documentation, especially the Django tutorial.

Your example should rather look like this example from the Django docs.

Here are some comments:

def create(request): if request.POST: form = ArticleForm(request.POST) if form.is_valid: form.save() # after successful POST # we want to redirect to a different page here else: form = ArticleForm() args = {} # you really don't need the following necessarily # just use `{% csrf_token %}` inside the form in your template args.update(csrf(request)) args['form'] = form # using just `render` like in the example linked to above is more modern return render_to_response('create_article.html', args) 

2 Comments

Okay perfect, thanks! so suppose the Article model has a field called name = models.CharField(max_length=20), is there a way for me to get / access what the user entered for that particular section of the form? Suppose I want to get the name and see if the name already exists in my database, how would I go about doing so?
Yes, of course. Don't confuse Django Forms and Models, they have different intentions (Forms=User Input Validation, Models=data persistence and access). Try the Django tutorial or the model/forms section of the docs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.