Brylie Christopher Oxley
Brylie Christopher Oxley

Reputation: 1872

How to use a Django model for validation and form generation in client-side application?

When designing a frontend webapp for a Django backend service, it would be nice to define the data model/validation code only once (using Django ORM).

Upvotes: 1

Views: 1137

Answers (3)

Debanjan Basu
Debanjan Basu

Reputation: 894

Not a complete answer, but model validation can be pushed to the client by using https://johnfraney.github.io/django-front-end-validators/

I would ideally fork this and implement what my project requires since this project is not mature enough for the projects I work in.

Upvotes: 1

davecaputo
davecaputo

Reputation: 341

I'm not aware of any such django library. Normally validation at the client side will require a JS library e.g. Parsely.js or validate.js (there are plenty!).

However, my experience is that you don't really need this if you want to keep your code simple, as you can set custom form/model validators in django directly, so that the form submitted will be rejected if any of the rules you specify are not met, and a custom error message displayed.

Nonetheless, the closest that I can think to what you might be looking for is the package django crispy forms which has a superb api and allows you to set most of your form html and form logic directly from the server side.

Hope this helps!

Upvotes: 3

Karim N Gorjux
Karim N Gorjux

Reputation: 3033

Django restframework has all what you need to serialize a model quickly. Have a look here: http://www.django-rest-framework.org/tutorial/1-serialization/

Upvotes: 1

Related Questions