1

I am confused about how to process my form(s) using django, django-rest-framework, angularjs, and django-angular.

Using standard Django techniques I can create a model form in my view and pass it to my template.

Using django-angular, I can ensure that the form has lots of pretty bootstrap3 styling and integrates well w/ angularjs.

Using angularjs, I can bind the form to javascript models and gets loads of cool interactive functionality.

Using django-rest-framework, I can load the initial form data via a RESTful API which returns JSON.

This all works great. I am just not sure what to do when submitting my form though...

Should I submit using an angular function that calls my RESTful API? Or should I submit using normal Django methods (ie: if form.is_valid(): form.save()?

I am refactoring this code from a pure Django app and the forms have some extremely complex custom validation methods. I am not sure that I can (or should) replicate that in angular.

Is there a "best practice" out there? Given that I can GET and POST via my RESTful API, what is the advantage to still doing it via Django?

note: I just thought that custom serialization validation should let me have the same level of complexity going through django-rest-framework as I previously had in pure Django. Whether this is a good idea or not is still a valid question.

note2: angularjs & django-rest-framework sure does seem faster.

1
  • I can't comment on the django specifically, but angular is a single page application - there should be no page reloading between actions. Submitting the form to your back end reloads the page. Stick with having angular call your REST api. Commented Sep 10, 2015 at 1:25

1 Answer 1

2

It is common practice to submit the data through the API and let the serializer do most of the validation for you. You can then do custom validation if needed. You can take a look here for more information on custom validation if you need it. But it does a pretty good job and I rarely have to write any custom validation.

Some of the problems you may run into with trying to do it through pure django, is that it will mess up your SPA. Doing a POST request will require you to "leave the SPA" and return back to it. Other issues such as CSRF tokens are also a pain in the butt to try and figure out. Best to just use your REST API.

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

2 Comments

I guess that makes sense. I now have code that correctly validates the serializer rather than the form. It correctly returns an error dictionary like {"field_name" : ["there is a problem w/ this field"]}, but I'm not sure how to add that error to the template (and have it rendered nicely via bootstrap). Perhaps that warrants its own question on SO.
Yeah, that could be its own question. An easy way to do it is just put an generic error above the form. Then in your call to the api, if it fails you can just se the ng-show to true. Really basic, really quick and easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.