Beginner: What are the pitfalls of using an html form without using django forms? I.e. form.py

58 views
Skip to first unread message

T Kwn

unread,
Dec 8, 2014, 8:37:44 PM12/8/14
to django...@googlegroups.com
I'm created a form where a user can add or remove other users from a group. Because the number of available users is unknown beforehand, I needed to create a checkbox form where the number of elements is dynamic. However, I couldn't figure out how to do it so I just used an html form and some <input type="checkbox">. I then processed the result in my view. I do not have any form defined in forms.py.

 I presume this is poor django practice but I'd like to know more details on why. Obviously I'm not getting the built-in form validation but with checkboxes it doesn't seem very necessary. Anything else?

My view can be seen here:

https://gist.github.com/anonymous/d1e8bd43c20eced9e4ce

The template code is pretty basic such as:

{% for myuser in members %}
{{ myuser.username }}
<input type="checkbox" value="{{ myuser.username }}" name="delete_me"/>
<br>
{% endfor %}



Daniel Roseman

unread,
Dec 9, 2014, 4:26:47 AM12/9/14
to django...@googlegroups.com
There's nothing wrong with doing that, of course. Just as you can write raw SQL to talk to your database, and even raw WSGI to power your web app. But Django provides utilities like the ORM, and the forms framework, to make it easier for you. If you find they don't help for your use case, then feel free not to use them.
--
DR.

T Kwn

unread,
Dec 9, 2014, 8:01:02 PM12/9/14
to django...@googlegroups.com
Thx for the explanation. I thought as much but as a beginner am always worried about some unforseen consequence.

mulianto

unread,
Dec 9, 2014, 10:43:46 PM12/9/14
to django...@googlegroups.com
Hi Tdkwon,

What the benefits using django forms something like :

1. You can reuse it on another view programmatic way, not copy paste html only
2. You have the definition of form in  1 place rather distributed on many views to change it.
3. You have the helptext, error message, validation, css class 
4.little code in views to process form data and save to models

Try it and tou will found it more usefull

Regards,

Mulianto


Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3e6d5ef4-ed66-46d1-9906-f2de823fb311%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages