LoginSignup
2
1

More than 3 years have passed since last update.

Djangoにおけるフォーム

Posted at

はじめに

ここでは、djangoにおけるフォームの設定の基本について解説します。

forms.pyの追加

forms.py
from django import forms

from .models import SampleModel


class SampleForm(forms.ModelForm):

    char_sample = forms.CharField(widget=forms.TextInput(attrs={'size': 300}))

    class Meta:
        model = SampleModel
        fields = ('char_sample', 'text_sample')

フォームのクラスを設定することで、モデルで設定したフォームにより細かな設定を行うことができます。
Metaクラスでは、対象とするモデルと、表示するフィールドを明記します。

まとめ

ここでは、Djangoのフォームのクラスの設定について解説しました。
次回はビューについて取り上げる予定です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1