Mark Score
Mark Score

Reputation: 62

Is it bad to design form layout in forms.py Django?

I'm new to Django. After few weeks learning, I've designed my form layout inside forms.py, like this: enter image description here

But it's quite annoying when I have to write html tag in string format without IDE styling, So do you guys have any ideas to design form layout in Django that is more clear and easy to maintain? I'm using crispy_forms btw. Thank you...

Upvotes: 0

Views: 255

Answers (1)

crimsonpython24
crimsonpython24

Reputation: 2383

It's not bad as there are no ways to tell whether practices are good or bad as they're mostly up to personal flavor. My take, however, is that crispy forms do not provide enough flexibility. Plus, once you moved on to complex form controls, you need multiple divisions and the hierarchy will be stacked.

It's up to you whether you want to keep this, but I'll tell you how I get past the forms.

  1. I render the form, no styling, and anything. I then inspect the page and then note down the id, name, field type (autocomplete, required, etc.) of every input as Django needs those to tell your data apart from others.
  2. I don't directly load the form like {{ form.as_p }}. I include the action, URL, and then apply my custom form but use the id, name, and everything else that the default form uses.

So you'll have the same Django field attributes but they are applied to your custom HTML form. It's definitely more work, but from my experience, this pays off.

Upvotes: 1

Related Questions