I am making a web page with Django.
I am new to this and in the course I took to learn, in the app I users
had a model and a form, in the model the fields are:
user
, phone_number
, picture
, created
andmodified
While in the form they are:
username
, password
, password_confirmation
, firs_name
, last_name
andemail
My question is: Why not put all this data in the models files? What exactly is the function of the form file? Cheers!
Is the data entered through the form saved in the database as well?
The forms in Django are in charge of handling, validating and "cleaning" the form data, the POST data specifically, in my opinion that is the main function, it has other functions, such as generating HTML code automatically, etc.
It's a bit complex, but behind the scenes, it POSTs data received (
MyForm(request.POST)
), validates it, and "cleans" it, i.e. normalizes the data as a Python object .After doing enough validations on fields, data, etc, then initialize the attributes in the object or in the instance, with the data already validated and clean, something like this:
Since I have read a bit of the source code, I know more or less how it works, so for you to better understand how it works, I have programmed a
ModelForm
super simple one, so you can understand:Now let's use that
CustomModelForm
:It would work the same as a
ModelForm
, with the differences that it has almost no validations, for example it does not validate that the attributefields
, of the classMeta
is a tuple or list and, in turn, verify that these attributes exist in the model, and so on. ..Well, the data as such, no, the object itself, that is, a record is created in the db, with said data .
Why is the form data not put in models?
Regarding this question, I don't understand it very well, but I think you are confused about the attribute
fields
of the classMeta
.The attribute
fields
in the forms is to specify which fields will be validated, initialized in the instance and automatically generated HTML code will be displayed.I'm not sure what exactly you mean, if you could clarify it would be ideal. I hope I've helped.
I'm new to django, maybe I won't give you the best explanation (many expert programmers will give you a technical explanation and if you can it would be better if you understood them, I'll give you the summary in Creole). In the model.py file is where you will assign your tables that will contain your database, that is, your photos, files, information, how the user will relate to it, etc. While the function of the Forms is to authenticate the user to be able to manipulate them in the admin or page, for example, in a blog, a user can upload a photo, information, etc. whatever you put in your database(models.py). In the admin you will see the Users option and there you will see all the users you have created and their administrative permissions, they will be able to delete, modify, etc in your model (database) if they have the required permissions.
Your question is what the forms are for, well they are like a file of a person, in the form you can ask for their full name, gender, email, telephone, etc. and that record is used to be able to relate to the database.