3

I'm about to create a form in my website made in Django with elements that have complex input types from different plugins (like calendar plugins). Also, I'll be using a Validator plugin which validates the input before submission.

My question is do I need to create forms.py and like model the form or can I just create the form manually? The former seems like a very hassle process. Which one is more efficient and recommended?

Jude Maranga
  • 865
  • 2
  • 9
  • 27
  • 2
    You can use your template's html form if you don't want Django to handle this form based on your models or widgets. Using forms.py is optional. As long as your form is submitted to a legitimate view handler, this should be completely up to you. – dmitryro Jan 01 '18 at 19:53
  • 1
    Check https://stackoverflow.com/questions/162159/javascript-client-side-vs-server-side-validation, don't use only client validation. – schrodingerscatcuriosity Jan 01 '18 at 21:17
  • @dmitryro I am actually building a form based on one of my models. So what I was trying to do, if I were not to use forms.py, is to make a form in my HTML template with names the same with my model and handle it in the views.py. Is this okay? – Jude Maranga Jan 02 '18 at 02:34
  • As long as you hit a view that knows how to read your parameters, this is completely up to you where this gets submitted, it can be virtually any form, what matters is the action and the right type `POST`, `GET`, or any other. Even curl. You might be required to provide csrf_token, if not exempt from check. – dmitryro Jan 02 '18 at 02:37
  • @dmitryro Oh I know it will work (in fact I've tried it in on one of my practices) but what I meant is that "is it a good practice or can it be considered a good practice?" – Jude Maranga Jan 02 '18 at 02:51
  • 1
    Good practice it to use only things that you really really need, not just as they are specified, hyped or popular. – dmitryro Jan 02 '18 at 02:53
  • @dmitryro well, good practices aren't just there because of popularity. It allows you to keep you code maintainable. For example the use of indentation in C is completely unnecessary and you can write your whole program in a single line but is a good practice the use of indentation – Mauricio Cortazar Jan 02 '18 at 09:49

2 Answers2

0

As @dmitryro said you can create your forms manually in the templates and then getting in the request. It's recommended to use the forms api provided by Django since it allows you to reuse, validate, and customize your forms.

As to whether or not it is a good practice that depends completely on you but if you are trying to scale an application I would recommend use the forms.

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27
  • But what if I don't need to reuse that form to another page? – Jude Maranga Jan 02 '18 at 12:29
  • @JudeMaranga as I said it has another uses and help you to save code, for example with model forms, somethings you don't want to write the forms because is to much work, so it depends on you and how the app scale – Mauricio Cortazar Jan 02 '18 at 16:43
0

It is good to use Django's built in form.

If we use django's form then we only have to write python code and django will create corresponding html for it. And our code will be short and clean.

  • 2
    But it's so hard to customize it most especially if you're using complex plugins for inputs like 3rd party calendar plugins – Jude Maranga Jan 02 '18 at 12:27
  • Exactly. I prefer writing my form in HTML, CSS and JS and just get the values from django – Mush Sep 30 '21 at 09:02