4

How do you avoid duplicating validation code on the server-side and on the client-side with Django?

Given that Django follows a Don't Repeat Yourself principle, it seems logical that there would be a way to do this, but I have no idea how.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 2
    You have to validate data on the server side because you can't trust anything from clients. Someone could easily bypass any validation you put into a web page. You also *should* validate data on the client - to make a more fast and pleasant user experience as well as slightly reducing the server load. Which of those two would you prefer to eliminate? – ratsbane Jul 09 '11 at 18:34
  • 3
    @Robus, @ratsbane: I think what I said had two interpretations, and I didn't mean the one you guys mentioned. :) I was referring to the duplication of the *my code*, not the duplication of the validation itself. i.e. how do you avoid repeating yourself (e.g. once in JavaScript, once in Python)? e.g., is there any way to automatically generate JavaScript validation based on Python code? Or write validation declaratively and generate both kinds of code from it? etc. – user541686 Jul 09 '11 at 19:01

1 Answers1

3

Here's an uncompleted ("alpha grade") project that addresses your problem: http://eikke.com/django-validation-an-introduction/ :

So I’ve been thinking since some time to create a Django templatetag which allows one to generate client-side Javascript form validation code without writing any code himself (unless using custom widgets).... The resulting project is called django-validation.

(And see also Django ticket #19)

Udi
  • 29,222
  • 9
  • 96
  • 129
  • As they closed the issue with `WONTFIX` it is up to us to implement until release of a good package. – brsbilgic Jul 10 '11 at 08:12