{"id":989,"date":"2024-12-13T11:20:53","date_gmt":"2024-12-13T03:20:53","guid":{"rendered":"http:\/\/www.chan.ink\/?p=989"},"modified":"2024-12-13T11:20:56","modified_gmt":"2024-12-13T03:20:56","slug":"%e3%80%90django%e3%80%91cleaned_data","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/13\/%e3%80%90django%e3%80%91cleaned_data\/","title":{"rendered":"\u3010Django\u3011cleaned_data"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Django \u7684\u8868\u5355\u5904\u7406\u4e2d\uff0c<code>cleaned_data<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u662f <strong>\u8868\u5355\u9a8c\u8bc1<\/strong> \u548c <strong>\u6570\u636e\u6e05\u6d17<\/strong> \u8fc7\u7a0b\u540e\u7684\u7ed3\u679c\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>cleaned_data<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e86\u7528\u6237\u901a\u8fc7\u8868\u5355\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u5e76\u4e14\u7ecf\u8fc7\u4e86 Django \u7684\u8868\u5355\u9a8c\u8bc1\u5668\u548c\u6e05\u6d17\u5668\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u8868\u5355\u9a8c\u8bc1\u4e0e\u6e05\u6d17<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Django \u4e2d\uff0c\u8868\u5355\uff08<code>Form<\/code>\uff09\u7684\u4e3b\u8981\u4f5c\u7528\u4e4b\u4e00\u662f\u9a8c\u8bc1\u548c\u6e05\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u3002<code>cleaned_data<\/code> \u662f\u7ecf\u8fc7\u8868\u5355\u7684 <code>clean()<\/code> \u65b9\u6cd5\u548c\u6bcf\u4e2a\u5b57\u6bb5\u7684 <code>clean()<\/code> \u65b9\u6cd5\u5904\u7406\u540e\u7684\u6700\u7ec8\u7ed3\u679c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9a8c\u8bc1<\/strong>\uff1a\u9996\u5148\uff0cDjango \u4f1a\u68c0\u67e5\u8868\u5355\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bb5\u662f\u5426\u6ee1\u8db3\u5b9a\u4e49\u7684\u7ea6\u675f\u6761\u4ef6\uff08\u4f8b\u5982\uff0c\u662f\u5426\u4e3a\u5fc5\u586b\u5b57\u6bb5\u3001\u5b57\u6bb5\u7c7b\u578b\u662f\u5426\u6b63\u786e\u3001\u662f\u5426\u6ee1\u8db3\u6700\u5927\u957f\u5ea6\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6e05\u6d17<\/strong>\uff1a\u9a8c\u8bc1\u901a\u8fc7\u540e\uff0cDjango \u4f1a\u5bf9\u5b57\u6bb5\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u6216\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u7684\u65e5\u671f\u8f6c\u6362\u4e3a <code>datetime<\/code> \u5bf9\u8c61\uff0c\u6216\u8005\u5c06\u8f93\u5165\u7684\u6587\u672c\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>cleaned_data<\/code> \u7684\u5f62\u6210<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5728\u89c6\u56fe\u4e2d\u8c03\u7528 <code>form.is_valid()<\/code> \u65f6\uff0cDjango \u4f1a\u5bf9\u8868\u5355\u8fdb\u884c\u9a8c\u8bc1\u3002\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\uff0c<code>form.cleaned_data<\/code> \u5c06\u5305\u542b\u6240\u6709\u7ecf\u8fc7\u9a8c\u8bc1\u548c\u6e05\u6d17\u540e\u7684\u6570\u636e\u3002\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\uff0c<code>form.errors<\/code> \u4f1a\u5305\u542b\u9519\u8bef\u4fe1\u606f\uff0c\u800c <code>cleaned_data<\/code> \u5219\u4e3a\u7a7a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5b9a\u4e49\u4e00\u4e2a\u8868\u5355\u5e76\u8bbf\u95ee <code>cleaned_data<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u5355\u6765\u5904\u7406\u7528\u6237\u7684\u59d3\u540d\u548c\u5e74\u9f84\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django import forms\n\nclass UserForm(forms.Form):\n    name = forms.CharField(max_length=100)\n    age = forms.IntegerField(min_value=18)\n\n    def clean_age(self):\n        # \u81ea\u5b9a\u4e49\u7684\u5b57\u6bb5\u6e05\u7406\u65b9\u6cd5\n        age = self.cleaned_data&#91;'age']\n        if age &lt; 18:\n            raise forms.ValidationError(\"\u5e74\u9f84\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e18\u5c81\")\n        return age\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528\u8868\u5355\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import render\nfrom .forms import UserForm\n\ndef user_view(request):\n    if request.method == \"POST\":\n        form = UserForm(request.POST)\n        if form.is_valid():\n            # \u901a\u8fc7\u9a8c\u8bc1\u5e76\u6e05\u6d17\u7684\u6570\u636e\n            cleaned_data = form.cleaned_data\n            name = cleaned_data&#91;'name']\n            age = cleaned_data&#91;'age']\n            return render(request, 'user_success.html', {'name': name, 'age': age})\n        else:\n            return render(request, 'user_form.html', {'form': form})\n    else:\n        form = UserForm()\n    return render(request, 'user_form.html', {'form': form})\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>cleaned_data<\/code> \u7684\u8be6\u7ec6\u89e3\u91ca<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9a8c\u8bc1\u540e\u7684\u6570\u636e<\/strong>\uff1a\u5728\u8868\u5355\u901a\u8fc7 <code>is_valid()<\/code> \u65b9\u6cd5\u9a8c\u8bc1\u540e\uff0c<code>cleaned_data<\/code> \u5c31\u4f1a\u5305\u542b\u6240\u6709\u6709\u6548\u7684\u6570\u636e\uff0c\u5e76\u4e14\u6240\u6709\u5b57\u6bb5\u6570\u636e\u90fd\u5df2\u7ecf\u8fc7\u4e86\u9a8c\u8bc1\u89c4\u5219\u7684\u68c0\u67e5\u3002<\/li>\n\n\n\n<li><strong>\u5b57\u6bb5\u6e05\u7406\u540e\u7684\u6570\u636e<\/strong>\uff1a\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\uff08\u5982\u679c\u9700\u8981\uff09\u53ef\u80fd\u5df2\u7ecf\u8fc7\u81ea\u5b9a\u4e49\u7684\u6e05\u7406\u65b9\u6cd5\uff08\u4f8b\u5982\uff0c<code>clean_name()<\/code> \u6216 <code>clean_age()<\/code>\uff09\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5305\u62ec\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3001\u6e05\u7406\u6570\u636e\uff08\u5982\u53bb\u9664\u7a7a\u683c\uff09\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5982\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u4e00\u4e2a <code>CharField<\/code> \u5b57\u6bb5\uff0c<code>cleaned_data['name']<\/code> \u53ef\u80fd\u5df2\u7ecf\u88ab\u4fee\u526a\u4e86\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e <code>DateField<\/code> \u5b57\u6bb5\uff0c<code>cleaned_data['birth_date']<\/code> \u53ef\u80fd\u5df2\u7ecf\u88ab\u8f6c\u6362\u4e3a Python \u7684 <code>datetime.date<\/code> \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e <code>IntegerField<\/code> \u5b57\u6bb5\uff0c<code>cleaned_data['age']<\/code> \u53ef\u80fd\u5df2\u7ecf\u88ab\u9a8c\u8bc1\u4e3a\u6574\u6570\uff0c\u5e76\u4e14\u5982\u679c\u9700\u8981\uff0c\u8fd8\u4f1a\u7ecf\u8fc7\u81ea\u5b9a\u4e49\u7684\u6e05\u7406\u65b9\u6cd5\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5982\u4f55\u8bbf\u95ee <code>cleaned_data<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u89c6\u56fe\u4e2d\uff0c\u5f53\u4f60\u8c03\u7528 <code>form.is_valid()<\/code> \u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>form.cleaned_data<\/code> \u8bbf\u95ee\u6e05\u6d17\u540e\u7684\u6570\u636e\u3002<code>cleaned_data<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u7684\u952e\u662f\u8868\u5355\u5b57\u6bb5\u7684\u540d\u79f0\uff0c\u503c\u662f\u5b57\u6bb5\u7684\u6e05\u6d17\u540e\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>if form.is_valid():\n    # \u83b7\u53d6\u7ecf\u8fc7\u6e05\u6d17\u548c\u9a8c\u8bc1\u7684\u6570\u636e\n    cleaned_data = form.cleaned_data\n\n    # \u83b7\u53d6\u5b57\u6bb5\u7684\u6570\u636e\n    name = cleaned_data&#91;'name']\n    age = cleaned_data&#91;'age']\n\n    # \u8fdb\u4e00\u6b65\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fdd\u5b58\u5230\u6570\u636e\u5e93\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5e38\u89c1\u7684\u6e05\u6d17\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Django \u8868\u5355\u4e2d\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u6709\u4e00\u4e2a <code>clean()<\/code> \u65b9\u6cd5\uff0c\u901a\u5e38\u7528\u6765\u6e05\u6d17\u5b57\u6bb5\u6570\u636e\u3002\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728\u8868\u5355\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 <code>clean_&lt;fieldname&gt;()<\/code> \u65b9\u6cd5\u6765\u5bf9\u67d0\u4e2a\u5b57\u6bb5\u8fdb\u884c\u81ea\u5b9a\u4e49\u7684\u9a8c\u8bc1\u548c\u6e05\u6d17\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u81ea\u5b9a\u4e49\u5b57\u6bb5\u6e05\u6d17<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserForm(forms.Form):\n    name = forms.CharField(max_length=100)\n    age = forms.IntegerField(min_value=18)\n\n    def clean_name(self):\n        # \u81ea\u5b9a\u4e49\u7684\u5b57\u6bb5\u6e05\u7406\u65b9\u6cd5\n        name = self.cleaned_data&#91;'name']\n        if \" \" in name:\n            raise forms.ValidationError(\"\u59d3\u540d\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\")\n        return name\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>clean_name()<\/code> \u4f1a\u9a8c\u8bc1 <code>name<\/code> \u5b57\u6bb5\u662f\u5426\u5305\u542b\u7a7a\u683c\uff0c\u5982\u679c\u5305\u542b\u7a7a\u683c\uff0c\u5219\u629b\u51fa\u9a8c\u8bc1\u9519\u8bef\u3002\u5982\u679c\u6ca1\u6709\u9519\u8bef\uff0c<code>name<\/code> \u5b57\u6bb5\u4f1a\u88ab\u8fd4\u56de\u6e05\u6d17\u540e\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong><code>cleaned_data<\/code> \u4e0e <code>form.cleaned_data<\/code> \u7684\u5173\u7cfb<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cleaned_data<\/code> \u662f <code>form.cleaned_data<\/code> \u7684\u6700\u7ec8\u7ed3\u679c\uff0c\u5b83\u662f\u8868\u5355\u9a8c\u8bc1\u6210\u529f\u540e\u7684\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u5728\u89c6\u56fe\u4e2d\u901a\u8fc7 <code>form.cleaned_data<\/code> \u8bbf\u95ee <code>cleaned_data<\/code>\uff0c\u8fd9\u4e2a\u5b57\u5178\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7ecf\u8fc7\u9a8c\u8bc1\u548c\u6e05\u6d17\u7684\u5b57\u6bb5\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>cleaned_data<\/code><\/strong> \u662f Django \u8868\u5355\u9a8c\u8bc1\u540e\u6e05\u6d17\u7684\u6570\u636e\u5b57\u5178\uff0c\u5305\u542b\u4e86\u6240\u6709\u5b57\u6bb5\u7684\u6570\u636e\uff08\u5982\u679c\u901a\u8fc7\u9a8c\u8bc1\uff09\u3002<\/li>\n\n\n\n<li><code>cleaned_data<\/code> \u662f\u5b57\u6bb5\u6570\u636e\u7ecf\u8fc7\u9a8c\u8bc1\u3001\u6e05\u7406\u548c\u8f6c\u6362\u540e\u7684\u7ed3\u679c\uff0c\u9002\u5408\u76f4\u63a5\u4f7f\u7528\uff0c\u4f8b\u5982\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><code>cleaned_data<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u662f\u5b57\u6bb5\u540d\uff0c\u503c\u662f\u6e05\u6d17\u540e\u7684\u5b57\u6bb5\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u5728\u8868\u5355\u7c7b\u4e2d\u81ea\u5b9a\u4e49 <code>clean_&lt;fieldname>()<\/code> \u65b9\u6cd5\u6765\u6267\u884c\u5b57\u6bb5\u7684\u989d\u5916\u9a8c\u8bc1\u548c\u6e05\u6d17\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <code>cleaned_data<\/code>\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u5730\u4f7f\u7528\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u62c5\u5fc3\u6570\u636e\u683c\u5f0f\u95ee\u9898\uff0c\u56e0\u4e3a Django \u5df2\u7ecf\u5bf9\u5b83\u8fdb\u884c\u4e86\u9a8c\u8bc1\u548c\u6e05\u6d17\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Django \u7684\u8868\u5355\u5904\u7406\u4e2d\uff0ccleaned_data \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u662f \u8868\u5355\u9a8c\u8bc1 \u548c \u6570\u636e\u6e05\u6d17  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-989","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=989"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/989\/revisions"}],"predecessor-version":[{"id":990,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/989\/revisions\/990"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=989"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}