{"id":180,"date":"2022-09-19T13:45:02","date_gmt":"2022-09-19T04:45:02","guid":{"rendered":"http:\/\/192.168.0.16:3005\/?p=180"},"modified":"2022-09-19T13:46:00","modified_gmt":"2022-09-19T04:46:00","slug":"python-django-modelform%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e9%a0%91%e5%bc%b5%e3%81%a3%e3%81%a6%e8%aa%bf%e3%81%b9%e3%81%a6%e6%99%82%e9%96%93%e6%b5%aa%e8%b2%bb%e3%81%99%e3%82%8b%e3%81%ae%e3%82%82","status":"publish","type":"post","link":"https:\/\/blog.solong.jp\/?p=180","title":{"rendered":"Python Django: ModelForm\u306e\u4f7f\u3044\u65b9\u9811\u5f35\u3063\u3066\u8abf\u3079\u3066\u6642\u9593\u6d6a\u8cbb\u3059\u308b\u306e\u3082\u3046\u3084\u3081\u308b"},"content":{"rendered":"\n<p>Django\u306eForm\u95a2\u9023\u306e\u6a5f\u80fd\u306f\u624b\u9593\u3092\u8272\u3005\u6e1b\u3089\u3057\u3066\u304f\u308c\u308b\u6c17\u304c\u3059\u308b\u306e\u3067\u3001\u305c\u3072\u4f7f\u3044\u65b9\u3092\u30de\u30b9\u30bf\u30fc\u3057\u3066\u6d3b\u7528\u3057\u305f\u3044\u306a\u3001\u3068\u601d\u3063\u3066\u3044\u305f\u3051\u3069\u3001\u304b\u3086\u3044\u3068\u3053\u308d\u306b\u624b\u304c\u5c4a\u304b\u3044\u306e\u3067\u3001\u4e00\u90e8\u3042\u304d\u3089\u3081\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;app\/form.py]\nfrom django import forms\nfrom .models import Project\n\nclass ProjectForm(forms.ModelForm):\n    class Meta:\n        model = Project\n        fields = &#91;\"field1\", \"field2\", \"field3\"]<\/code><\/pre>\n\n\n\n<p>\u3053\u308c\u3060\u3051\u3067Form\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u305d\u306e\u3082\u306e\u306f\u3067\u304d\u3061\u3083\u3046\u306e\u3067\u3001View\u306b1\u884c\u304f\u3089\u3044\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u6e21\u3059\u51e6\u7406\u66f8\u3044\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u3001Template\u5074\u3067\u306f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        &lt;form method=\"POST\"&gt; {% csrf_token %}\n            &lt;fieldset class=\"module aligned\"&gt;\n                {% for field in form %}\n                &lt;div class=\"form-row\"&gt;&lt;div&gt;\n                    &lt;label&gt;{{ field.label_tag }}&lt;\/label&gt;\n                    &lt;div class=\"related-widget-wrapper\"&gt;\n                        {{ field }}\n                        {{ field.errors }}\n                    &lt;\/div&gt;\n                &lt;\/div&gt;&lt;\/div&gt;\n                {% endfor %}\n                &lt;input name=\"submit\" type=\"submit\" value=\"Regist\" \/&gt;\n            &lt;\/fieldset&gt;\n        &lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>\u3053\u308c\u66f8\u3044\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u30d5\u30a9\u30fc\u30e0\u4f5c\u3063\u3066\u304f\u308c\u3061\u3083\u3046\u3057\u3001\u3053\u308a\u3083\u4eca\u5f8c\u30d5\u30a3\u30fc\u30eb\u30c9\u5897\u3048\u305f\u3068\u304d\u3082\u4fbf\u5229\u3060\u306a\u3042\u3068\u78ba\u304b\u306b\u52a9\u304b\u3063\u305f\u3093\u3060\u3051\u3069\u3082\u3001View\u5074\u3067POST\u3092\u53d7\u3051\u53d6\u3063\u305f\u3068\u304d\u304c\u56f0\u308a\u7269\u3067<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    if(request.method == 'POST'):\n        form = ProjectForm(request.POST)\n        if not form.is_valid():<\/code><\/pre>\n\n\n\n<p>\u3053\u308c\u3001Form\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u304b\u3089\u306f\u3042\u3048\u3066\u524a\u3063\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u3064\u3044\u3066\u306f\u3001\u624b\u3067\u6a2a\u304b\u3089\u30bb\u30c3\u30c8\u3057\u3066\u3042\u3052\u308b\u3053\u3068\u304c\u591a\u5206\u3067\u304d\u306a\u3044\u3093\u3058\u3083\u306a\u3044\u3060\u308d\u3046\u304b\u3002\u7121\u7406\u3084\u308arequest.POST\u306e\u4e2d\u8eab\u3092\u3044\u3058\u3063\u3066\u304b\u3089\u51e6\u7406\u3059\u308b\u306e\u3082\u306a\u3093\u304b\u9055\u3046\u3002\u4e0a\u8a18\u306e\u4f8b\u3060\u3068form\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066\u306a\u3093\u304b\u30e1\u30bd\u30c3\u30c9\u3084\u3089\u3067set\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u306e\u304b\u3068\u8abf\u3079\u305f\u3082\u306e\u306e\u51fa\u3066\u3053\u306a\u3044\u3057\u3001Validation\u306a\u3093\u304b\u3082\u602a\u3057\u3044\u3068\u3053\u308d\u3060\u30fb\u30fb\u30fb\u3002<\/p>\n\n\n\n<p>\u8272\u3005\u8003\u3048\u305f\u3059\u3048\u3001ModelForm\u306f\u753b\u9762\u306e\u751f\u6210\u3068\u4e00\u5b9a\u306eValidation\u3067\u306f\u983c\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3059\u308b\u3051\u3069\u3001\u30c7\u30fc\u30bf\u7de8\u96c6\u3057\u3066DB\u306b\u30ec\u30b3\u30fc\u30c9\u4fdd\u5b58\u3059\u308b\u51e6\u7406\u306fModel\u3067\u76f4\u3067\u66f8\u3044\u3066\u3042\u3052\u308b\u3053\u3068\u306b\u3059\u308b\u3002\u3053\u308c\u306f\u3082\u3046\u305d\u306e\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3002<\/p>\n\n\n\n<p>\u3057\u305f\u3089\u3001View\u306f\u3053\u3093\u306a\u3053\u3068\u306b\u306a\u3063\u305f\u3002\u30d4\u30a8\u30f3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    if(request.method == 'POST'):\n        form = ProjectForm(request.POST)\n        if not form.is_valid():\n            ctx&#91;'form'] = form\n        else:\n            p = request.POST\n            pj = Project(\n                field1=p&#91;'field1'],\n                field2=p&#91;'field2'],\n                field3=p&#91;'field3'],\n                field4=xxxxxxx,  # Form\u304b\u3089\u3067\u306f\u306a\u304f\u81ea\u5206\u3067\u5225\u306e\u65b9\u6cd5\u3067\u53d6\u5f97\u3055\u305b\u308b\n                field5=xxxxxxx,  # Form\u304b\u3089\u3067\u306f\u306a\u304f\u81ea\u5206\u3067\u5225\u306e\u65b9\u6cd5\u3067\u53d6\u5f97\u3055\u305b\u308b\n            )\n            pj.save()\n            messages.success(request, 'Success')\n            return redirect('xxxx:xxxxx')<\/code><\/pre>\n\n\n\n<p>\u3044\u3044\u306e\u304b\u306a\u3053\u308c\u3002\u3061\u3083\u3093\u3068\u52d5\u3044\u3066\u3001\u3042\u3068\u304b\u3089\u30e1\u30f3\u30c6\u3067\u304d\u308c\u3070\u3044\u3044\u3063\u3066\u3044\u3046\u30dd\u30ea\u30b7\u30fc\u3067\u3084\u3063\u3066\u308b\u3051\u3069\u3001\u4e0d\u5b89\u3060\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u306eForm\u95a2\u9023\u306e\u6a5f\u80fd\u306f\u624b\u9593\u3092\u8272\u3005\u6e1b\u3089\u3057\u3066\u304f\u308c\u308b\u6c17\u304c\u3059\u308b\u306e\u3067\u3001\u305c\u3072\u4f7f\u3044\u65b9\u3092\u30de\u30b9\u30bf\u30fc\u3057\u3066\u6d3b\u7528\u3057\u305f\u3044\u306a\u3001\u3068\u601d\u3063\u3066\u3044\u305f\u3051\u3069\u3001\u304b\u3086\u3044\u3068\u3053\u308d\u306b\u624b\u304c\u5c4a\u304b\u3044\u306e\u3067\u3001\u4e00\u90e8\u3042\u304d\u3089\u3081\u308b\u3002 \u3053\u308c\u3060\u3051\u3067Form\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u305d\u306e\u3082\u306e\u306f\u3067\u304d\u3061\u3083\u3046\u306e\u3067\u3001View\u306b1\u884c\u304f\u3089\u3044\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u6e21\u3059\u51e6\u7406\u66f8\u3044\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u3001Template\u5074\u3067\u306f \u3053\u308c\u66f8\u3044\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u30d5\u30a9\u30fc\u30e0\u4f5c\u3063\u3066\u304f\u308c\u3061\u3083\u3046\u3057\u3001\u3053\u308a\u3083\u4eca\u5f8c\u30d5\u30a3\u30fc\u30eb\u30c9\u5897\u3048\u305f\u3068\u304d\u3082\u4fbf\u5229\u3060\u306a\u3042\u3068\u78ba\u304b\u306b\u52a9\u304b\u3063\u305f\u3093\u3060\u3051\u3069\u3082\u3001View\u5074\u3067POST\u3092\u53d7\u3051\u53d6\u3063\u305f\u3068\u304d\u304c\u56f0\u308a\u7269\u3067 \u3053\u308c\u3001Form\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u304b\u3089\u306f\u3042\u3048\u3066\u524a\u3063\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u3064\u3044\u3066\u306f\u3001\u624b\u3067\u6a2a\u304b\u3089\u30bb\u30c3\u30c8\u3057\u3066\u3042\u3052\u308b\u3053\u3068\u304c\u591a\u5206\u3067\u304d\u306a\u3044\u3093\u3058\u3083\u306a\u3044\u3060\u308d\u3046\u304b\u3002\u7121\u7406\u3084\u308arequest.POST\u306e\u4e2d\u8eab\u3092\u3044\u3058\u3063\u3066\u304b\u3089\u51e6\u7406\u3059\u308b\u306e\u3082\u306a\u3093\u304b\u9055\u3046\u3002\u4e0a\u8a18\u306e\u4f8b\u3060\u3068form\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066\u306a\u3093\u304b\u30e1\u30bd\u30c3\u30c9\u3084\u3089\u3067set\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u306e\u304b\u3068\u8abf\u3079\u305f\u3082\u306e\u306e\u51fa\u3066\u3053\u306a\u3044\u3057\u3001Validation\u306a\u3093\u304b\u3082\u602a\u3057\u3044\u3068\u3053\u308d\u3060\u30fb\u30fb\u30fb\u3002 \u8272\u3005\u8003\u3048\u305f\u3059\u3048\u3001ModelForm\u306f\u753b\u9762\u306e\u751f\u6210\u3068\u4e00\u5b9a\u306eValidation\u3067\u306f\u983c\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3059\u308b\u3051\u3069\u3001\u30c7\u30fc\u30bf\u7de8\u96c6\u3057\u3066DB\u306b\u30ec\u30b3\u30fc\u30c9\u4fdd\u5b58\u3059\u308b\u51e6\u7406\u306fModel\u3067\u76f4\u3067\u66f8\u3044\u3066\u3042\u3052\u308b\u3053\u3068\u306b\u3059\u308b\u3002\u3053\u308c\u306f\u3082\u3046\u305d\u306e\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3002 \u3057\u305f\u3089\u3001View\u306f\u3053\u3093\u306a\u3053\u3068\u306b\u306a\u3063\u305f\u3002\u30d4\u30a8\u30f3\u3002 \u3044\u3044\u306e\u304b\u306a\u3053\u308c\u3002\u3061\u3083\u3093\u3068\u52d5\u3044\u3066\u3001\u3042\u3068\u304b\u3089\u30e1\u30f3\u30c6\u3067\u304d\u308c\u3070\u3044\u3044\u3063\u3066\u3044\u3046\u30dd\u30ea\u30b7\u30fc\u3067\u3084\u3063\u3066\u308b\u3051\u3069\u3001\u4e0d\u5b89\u3060\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":2,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/blog.solong.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.solong.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}