{"id":1146,"date":"2024-12-23T17:04:12","date_gmt":"2024-12-23T09:04:12","guid":{"rendered":"http:\/\/www.chan.ink\/?p=1146"},"modified":"2024-12-23T17:04:14","modified_gmt":"2024-12-23T09:04:14","slug":"%e3%80%90django%e6%a8%a1%e6%9d%bf%e8%af%ad%e6%b3%95%e3%80%91%e5%9f%ba%e7%a1%80%e7%9a%84%e6%a8%a1%e6%9d%bf%e8%af%ad%e6%b3%95%e4%bd%bf%e7%94%a8%e8%af%a6%e6%83%85","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/23\/%e3%80%90django%e6%a8%a1%e6%9d%bf%e8%af%ad%e6%b3%95%e3%80%91%e5%9f%ba%e7%a1%80%e7%9a%84%e6%a8%a1%e6%9d%bf%e8%af%ad%e6%b3%95%e4%bd%bf%e7%94%a8%e8%af%a6%e6%83%85\/","title":{"rendered":"\u3010Django|\u6a21\u677f\u8bed\u6cd5\u3011\u57fa\u7840\u7684\u6a21\u677f\u8bed\u6cd5\u4f7f\u7528\u8be6\u60c5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>class Event(object):\n\n    def __init__(self,name,age):\n        self.name = name\n        self.age = age\n\n    def get_data(self):\n        return '\u54c8\u54c8\u54c8\u54c8\u54c8'\n\n\ndef fetch_data():\n    return '\u6211\u662f\u4e0a\u5e1d'\n\ndef gen_data():\n    yield 123\n    yield 456\n    yield 789\n\ndef tpl(request):\n\n    name = '\u5b8b\u4e00\u5764'\n    roles = &#91;'\u7537\u4e3b\u4eba\u516c','\u5973\u4e3b\u4eba\u516c','\u91cd\u8981\u89d2\u8272']\n\n    user_info = {'name':'\u5b8b\u4e00\u5764','salary':1000000,'role':'\u7537\u4e3b\u4eba\u516c'}\n\n    data_list = &#91;\n        {'name': '\u5b8b\u4e00\u5764', 'salary': 1000000, 'role': '\u7537\u4e3b\u4eba\u516c'},\n        {'name': '\u590f\u82f1\u6770', 'salary': 8000, 'role': '\u5973\u4e3b\u4eba\u516c'},\n        {'name': '\u53f6\u7ea2\u519b', 'salary': 900000, 'role': '\u7537\u4e3b\u4eba\u516c\u670b\u53cb'},\n        {'name': '\u65b9\u5b50\u4e91', 'salary': 1000, 'role': '\u7537\u4e3b\u4eba\u516c\u670b\u53cb'},\n        {'name': '\u6c5f\u8587', 'salary': 2000, 'role': '\u5973\u4e3b\u4eba\u516c\u670b\u53cb'},\n    ]\n\n    event = Event('\u65b9\u5b50\u4e91',38)\n    return render(request,'app01\/tpl.html',\n                  {'n1':name,'n2':roles,'n3':user_info,'n4':data_list,'n5':event,'n6':fetch_data,'n7':gen_data})<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>Title&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;h1>\u6a21\u677f\u8bed\u6cd5\u5b66\u4e60&lt;\/h1>\n    &lt;div>{{ n1 }}&lt;\/div>\n    &lt;div>{{ n2 }}&lt;\/div>\n    &lt;div>{{ n2.0 }}&lt;\/div>\n    &lt;div>{{ n2.1 }}&lt;\/div>\n    &lt;div>{{ n2.2 }}&lt;\/div>\n&lt;hr\/>\n&lt;div>\n    {% for item in n2 %}\n    &lt;span>{{ item }}&lt;\/span>\n    {% endfor %}\n&lt;\/div>\n&lt;hr\/>\n&lt;div>{{ n3 }}&lt;\/div>\n&lt;hr\/>\n&lt;div>{{ n3.name }}&lt;\/div>\n&lt;div>{{ n3.salary }}&lt;\/div>\n&lt;div>{{ n3.role }}&lt;\/div>\n&lt;hr\/>\n&lt;ul>\n    {% for item in n3 %}\n    &lt;li>{{ item }}&lt;\/li>\n    {% endfor %}\n&lt;\/ul>\n&lt;hr\/>\n&lt;ul>\n    {% for item in n3.keys %}\n    &lt;li>{{ item }}&lt;\/li>\n    {% endfor %}\n&lt;\/ul>\n&lt;hr\/>\n&lt;ul>\n    {% for item in n3.values %}\n    &lt;li>{{ item }}&lt;\/li>\n    {% endfor %}\n&lt;\/ul>\n&lt;hr\/>\n&lt;ul>\n    {% for k,v in n3.items %}\n    &lt;li>{{ k }} : {{ v }}&lt;\/li>\n    {% endfor %}\n&lt;\/ul>\n&lt;hr\/>\n{{ n4 }}\n&lt;hr\/>\n{{ n4.0 }}\n{{ n4.1 }}\n{{ n4.2 }}\n{{ n4.3 }}\n&lt;hr\/>\n{{ n4.0.name }}\n{{ n4.0.salary }}\n{{ n4.0.role }}\n&lt;hr\/>\n\n{% for item in n4 %}\n    &lt;div>{{ item.name }}&lt;\/div>\n    &lt;div>{{ item.salary }}&lt;\/div>\n{% endfor %}\n&lt;hr\/>\n{{ n5.name }}\n{{ n5.age }}\n{{ n5.get_data }}\n&lt;hr\/>\n{{ n6 }}\n&lt;hr\/>\n{{ n7 }}\n&lt;hr\/>\n&lt;ul>\n    {% for item in n7 %}\n    &lt;li>{{ item }}&lt;\/li>\n    {% endfor %}\n&lt;\/ul>\n&lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"","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-1146","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1146","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=1146"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":1147,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1146\/revisions\/1147"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}