Do I need to create forms.py for my forms in Django? - django

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?

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.

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.

Related

Is there a way to store form layout in order to generate form dynamically in django mvt?

I'm working on a pretty large Django project, which has over 60 different forms layout (can be more than this when needed). I pretty confuse that how can I build all such forms manually??. I've came up with an idea is that I'm gonna store form layout in database and with every new forms, I just need to config in database and then using crispy layout to dynamically generate those forms...
Do you guys have any better ideas?
Thanks
When I worked on a form heavy project I used to rely on mixin.
Try to identify what are the comonly used type of field and create them in mixins.
If it's a lot of them then create some 'base' forms that incorporate certain set of fields.
Then you just have to compose the form you want from the diffrents mixins and bases and add anything specific to the new one you want to create.
Or you could create your own mapping structure for form and store the logic on how to build your form in a JsonField for exemple.
a json containing such thing as name, label, widgets, placeholder, (and whatever you need). It can be really simple or really complex if you have complicated structure...
Crispy-forms has dynamic layouts which may help for this use case.
https://django-crispy-forms.readthedocs.io/en/latest/dynamic_layouts.html

Storing value in django models without using Model forms

I am having very complex models in my project, that I cannot display easily on HTML page using modelForms.
So I want to to use normal forms instead and store the values in Models.is it possible?
It is possible. You simply create your own form by inheriting forms.Form.
Then get the form input from request in your views and create your object.
In short what you are asking is possible.
Here is the official documentation for forms: https://docs.djangoproject.com/en/1.8/topics/forms/

Creating templates for authentication forms in Django 1.7

First off, I'm very much a newbie when it comes to Django.
The problem I'm struggling with is in trying to create templates for the built-in authentication forms in Django 1.7 but there is very little that I can find in the way of concrete examples anywhere in the documentation or elsewhere.
I can find plenty of questions and examples that describe how to manually create the templates (or copy them from the Django packages) but from what I understand about the Form class and the built-in authentication forms is that I don't need to manually create the actual form in the template. In fact, it seems more desirable to use the Form classes because that would add ensure that fields are named correctly, that the validation such as max length on text fields is applied, etc.
Can anyone point me to some concrete examples or documentation of what I'm talking about? I've read the following sections already,
https://docs.djangoproject.com/en/1.7/topics/forms/
https://docs.djangoproject.com/en/1.7/topics/auth/default/
https://docs.djangoproject.com/en/1.7/topics/auth/customizing/
In fact, I would say that this question extends to any Form class in general but I'm specifically looking at the authentication forms here because I obviously haven't written these Form classes.
I don't think it should make any difference from what I've read since the same process should apply, but I will say that I'm using the django-authtools package (http://django-authtools.readthedocs.org/en/latest/)
I have the actual authentication system working fine. It logs users in and out, I can enforce that certain urls require the user to be logged in first, etc. so its only the actual form display that is an issue.

When to use form vs model validation?

Just curious. What is the best practice for when to use form vs model validation?
From what I understand currently, form validation should be used for:
AJAX / HTTP requests params
Forms that do not correlate to a model?
Another question is: I have a HTML form that roughly correlates to a model instance, do I use a ModelForm for it?
Definitely use ModelForm, if your form resembles model object even in a tiny bit.
If there are some minor differences (e.g. you don't use some of the fields or you want to use different error messages etc.) it's much easier to customize ModelForm then to use Form and implement all this functionality from scratch.
For more reference regarding ModelForm please checkout PyDanny's Core Concepts of Django ModelForms.
I am also trying to understand what is the difference/relation between form and model validation and I would like to share my notes that are formed after reading several docs.
I am currently interested in Creating Forms from Models
#mariodev shared the document Core Concepts of Django ModelForms and this provided a good start.
ModelForms select validators based off of Model field definitions
The main story behind the scenes seems to be the DRY principal. This article explains very well what exactly is the case here.
All right, all this is fair. The question is "Where in the Django Documentation is this explained"?
I bumped on a very brilliant article where it states that:
The form.full_clean() method is called by django at the start of the validation process (by form.is_valid(), usually immediately after the view receives the posted data).
Correct me if I am wrong but that line reads that everytime I enter data and hit 'enter' the validation process begins!
OK, this is simple now:
The validation on a ModelForm begins when we hit 'enter'.
Django first validates the form by checking one by one every applicable validation method on Fields, Field Subclasses (This is the documentation for a model's field subclass, not for a form field subclass), Form Subclasses and ModelForm (since it is a ModelForm).
Finally, it validates the Model Instance.
This is how all this works theoretically. The only thing that remains is to implement it.

Django - When to use Forms and Best Practices

Please share your thoughts abouts the following
When to use Django Forms to produce the HTML fields
When to avoid it and use the plain HTML
Any other tips and best practices
I use django forms or maybe another forms helper if I need something specific in every case, no matter what. I never compose forms using plain-old html.
Many aspects of form processing are not related to presentation. What kind of information needs to be collected and how to validate that certainly falls outside of the domain of presentation. Using a forms helper can help to unify all of this work in a pretty convenient way.
The fact that a forms helper can also render html is sort of coincidental to it's use. Certainly, if that's all they did, they wouldn't be worth much, but since they do all of that and stay in sync with the needs of the business logic, it somewhat requires using the html rendering from the form helper to reap the maximum benefit from the assistance it offers the rest of the app.
When to use Django Forms to produce the HTML fields
Django forms provide HTML forms for models, user-built as well as combination of both. One should be using Django forms most of the times since it considerably reduces the redundant templating effort. The tightly controlled security provided by Django forms along with the strong validation support is worth the effort to use Django forms.
When to avoid it and use the plain HTML
A good use-case to avoid Django forms is when you need to fire javascript events or there is a lot of style deviation from your main stylesheet.
Any other tips and best practices
Derive maximum advantages of the framework by using maximum features of the framework as possible.