Jason Howard
Jason Howard

Reputation: 1586

Function based views specifically for ajax calls

I've setup a number of function based views that are ONLY called by ajax calls. These ajax calls only pass a portion of the form data into the view. I've got a couple of questions regarding this.

1) I implemented an 'if form.is_valid()' after checking to see if request.is_ajax().

if request.is_ajax():
   if form.is_valid():
      all of my code in this Function Base View

The including of the form.is_valid() conditional prevented my code from running. How important is checking if the form is valid? Can this validation be done when I'm only passing some form fields into the view?

2) My ajax call is a POST call. How important is it to put if request.METHOD == 'POST' above my code in the function based view? I believe that the call will always be POST, so is there a reason to check for it? Does not checking for it represent a security concern?

Thanks!

Upvotes: 0

Views: 36

Answers (1)

masaterofnothing
masaterofnothing

Reputation: 217

All of these checks are a design question. You obviously can omit them, but probably your code will be easier to break. Validate is important to prevent the good execution of the query to store data.

I prefer the CBV approach to Django. Because you can reuse the generics views and with the OOP you can easily override its methods to do what you want

Upvotes: 0

Related Questions