[PYTHON] Django Form Gleaning

I'm sorry for the continuous throwing. I wanted to fill the calendar ...

About this article

I've been writing all about Form,

I'm writing about

ModelForm.field_order

A field called field_order in Model Form? there is. This is a field that literally sets the order of the fields.

forms.py


class MyForm(ModelForm):
    field_order = ["name", "age", "address", ]

If you do, the order of the fields will be set. If you want to decide only the first field, you can write only that field. The rest is determined by Django.

that way,

form.html


<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">
</form>

Even when you do like this, the order will be as you like.

crispy form

I often use Twitter Bootstrap, but I can't specify the CSS class in{{form}}. In that case, use the cripsy form.

Documentation

pip install django-crispy-forms

You can enter with this.

After that, in settings.py

settings.py


INSTALLED_APPS += ('crispy_forms', )
CRISPY_TEMPLATE_PACK = 'bootstrap3'

As in the template

form.html


{% load crispy_forms_tags %}
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">
</form>

If so, it's strange. It will be a properly styled form.

See the documentation for more details.

Also, there was a crispy form screencast in GoDjango, so I'll put a link here. I use it a little deeper than the one I introduced.

If you use the order of the fields I wrote at the beginning and the crispy form, the layout of the form will be much easier.

Especially the crispy form is a recommended module because it can do a lot of things.

Finally

I'm out of material.

Recommended Posts

Django Form Gleaning
Django Contact Form 2
Django Suggested Form
Django contact form
Django
Dynamically add form fields in Django
Get the value from the [Django] Form
django update
Django note 4
Django memorandum
django search
Django installation
Django Summary
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
django notes
Django basics
Django Shoho
Django defaults
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
DJango Note: From the beginning (form processing)
Django note 2
Get only the text from the Django form.
Django startup
Django notes
Django NullCharField
Dynamically add fields to Form objects in Django
Save multiple models in one form with Django