-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Questions on how to organize properly django forms #166
Comments
@EmanueleSarr Hello 👋 When it comes to forms, we usually look at them as serializers:
On a high-level, that's pretty much it. Now, forms are baked-in the more generic Django Views. That's why you may want to use something more simple, such as Let me know if this is enough 👍 |
Hello, thank you for the answer, however, I still do not know if I should put the presentation logic on the form, or not. Some things but I think you can not do menop that you do not put part of the presentation logic on the form, For example if I have to create placeholders from a dictionary (where the keys are field names and the values are placeholders to apply) that is only possible on forms, and not on templates, however this would lead to having the presentation logic both on the form, is on the template. |
@EmanueleSarr I think I get what you are asking, but if I see a code example, it'll be really helpful 🙏 Nevertheless, this can be considered a problem with Django forms, because some of the presentation logic can be expressed in the abstraction itself, on Python level. And some of the presentation logic can be expressed in the HTML / Django templates. Again, this can be considered a problem, but also, it can be considered "by design" - it's up to you what to use. My general approach in such scenarios is the following: Can I do everything in one place? If yes - I'd stick to that, no matter the place, until I reach an example that no-longer fits my assumptions and framework. The Django Forms are quite the powerful abstraction and I'll also look there first, especially if I'm not rendering the forms field by field, but rather simply as:
or if you are using other 3rd party packages that deals with that. If you are rendering the form, piece by piece, where most of the control lies on the frontend / tempalting side of things - then - I'd think about moving some of the presentation logic there. But again, if you give me few examples, I'll have a better idea and give you a better answer. Cheers! |
Thank you for your answer, I understand. form.html:
forms.py:
field.html:
|
@EmanueleSarr thank you for the extensive example. From what I see - you require a lot of flexibility, so rendering the form, piece by piece, in the HTML, looks like the right way to do it. If you want to move that flexibility to the Python side, you'll effectively have to implement your own small version of something that resembles My suggestion would be to try to do a POC and see how hard it's going to be to achieve what you want. You can always read the implementation of If not - the approach that you have - it's ok. As long as you separate your forms into small & includable HTML files, I think you'll be able to manage. In fact, if you go this route, I'd suggest looking at something like https://mitchel.me/slippers/docs/introduction/ (and the other similar options) - so you can have a more component-based approach with the templates. Cheers! |
All clear, thank you. I think I’ll go for django-crispy-forms, going to create a custom template, so that I can be flexible, and at the same time keep all the presentation logic in one place, in this case the django forms (python side) |
@EmanueleSarr Sounds like a good approach 👍 Best of luck! |
Since the guide does not go into the detail of the forms, I wonder what would be the best practices to create them. Personally, I do not like the fact that they mix the logic of validation and presentation, what would be the optimal way to manage it?
For example, if I want to insert placeholders on a form in an automatic way (without making each field individually in the model) I must necessarily put the logic or in the form or in the view, and frankly the cleaner solution seems to me to put it in the form, However, as I said above, it does not seem to me the greatest of solutions, I look for suggestions.
Thank you in advance.
The text was updated successfully, but these errors were encountered: