{"id":7403,"date":"2023-04-14T17:28:51","date_gmt":"2023-04-14T09:28:51","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7403"},"modified":"2023-04-17T14:52:27","modified_gmt":"2023-04-17T06:52:27","slug":"%e7%ac%ac%e4%b8%80%e4%b8%aadjango%e5%ba%94%e7%94%a8part2","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7403","title":{"rendered":"\u7b2c\u4e00\u4e2adjango\u5e94\u7528[part2]"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u8bbe\u7f6e\u6570\u636e\u5e93<\/h2>\n\n\n\n<p>\u6253\u5f00mysite\/settings.py\uff0c\u5728\u8be5\u6587\u4ef6\u4e2d\u63cf\u8ff0\u4e86django\u7684\u8bbe\u7f6e\u3002django\u9ed8\u8ba4\u4f7f\u7528\u7684\u662fSQLite\uff0c\u5982\u679c\u53ea\u662f\u4e3a\u4e86\u5b66\u4e60django\uff0c\u8fd9\u662f\u4e2a\u5f88\u597d\u7684\u5f00\u59cb\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5e0c\u671b\u4f7f\u7528\u5176\u5b83\u6570\u636e\u5e93\uff0c\u90a3\u5c31\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684database bindings\uff0c\u5e76\u6539\u53d8<strong>DATABASES<\/strong>\u4e2d\u7684<strong>&#8216;default\u2018<\/strong>\u9879\uff0c\u7528\u4e8e\u5339\u914d\u6570\u636e\u5e93\u7684\u8054\u63a5\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ENGINE<\/strong>\uff1a\u53ef\u4ee5\u662f&#8217;django.db.backends.sqlite3&#8217;\u3001&#8217;django.db.backends.postgresql&#8217;\u3001&#8217;django.db.backends.mysql&#8217;\u6216&#8217;django.db.backends.oracle&#8217;\u7b49\u5176\u5b83\u540e\u7aef\u540d\u5b57\uff0c\u8fd9\u548c\u4f7f\u7528\u7684\u6570\u636e\u5e93\u6709\u5173\u3002<\/li>\n\n\n\n<li><strong>NAME<\/strong>\uff1a\u6570\u636e\u5e93\u7684\u540d\u5b57\u3002\u5982\u679c\u4f7f\u7528\u7684\u662fSQLite\uff0c\u6570\u636e\u5e93\u4f1a\u662f\u7535\u8111\u4e0a\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u6240\u4ee5NAME\u5e94\u8be5\u662f\u5b8c\u6574\u7684\u7edd\u5bf9\u8def\u5f84\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8bbe\u7f6emysite\/settings.py\u4e2d\u7684<strong>TIME_ZONE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TIME_ZONE = 'Asia\/Shanghai'<\/code><\/pre>\n\n\n\n<p><strong>INSTALLED_APPS<\/strong>\u4fdd\u5b58\u4e86\u6240\u6709django\u5df2\u7ecf\u6fc0\u6d3b\u5e94\u7528\u7684\u540d\u5b57\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<strong>INSTALLED_APPS<\/strong>\u5305\u542b\u4e86\u4e0b\u9762\u7684\u5e94\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>django.contrib.admin <\/strong>\uff1a\u7ba1\u7406\u7ad9\u70b9<\/li>\n\n\n\n<li><strong>django.contrib.auth <\/strong>\uff1a\u8ba4\u8bc1\u7cfb\u7edf<\/li>\n\n\n\n<li><strong>django.contrib.contenttypes<\/strong> \uff1a\u5185\u5bb9\u7c7b\u578b\u6846\u67b6<\/li>\n\n\n\n<li><strong>django.contrib.sessions <\/strong>\uff1a\u4f1a\u8bdd\u6846\u67b6<\/li>\n\n\n\n<li><strong>django.contrib.messages<\/strong> \uff1a\u6d88\u606f\u6846\u67b6<\/li>\n\n\n\n<li><strong>django.contrib.staticfiles <\/strong>\uff1a\u7ba1\u7406\u9759\u6001\u6587\u4ef6\u7684\u6846\u67b6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5b83\u4eec\u4e2d\u7684\u4e00\u4e9b\u5e94\u7528\u81f3\u5c11\u8981\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u6240\u4ee5\u73b0\u5728\u9700\u8981\u5efa\u7acb\u6570\u636e\u8868\uff0c\u4e3a\u4e86\u4ee5\u540e\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py migrate<\/code><\/pre>\n\n\n\n<p><em>migrate<\/em>\u4f1a\u6839\u636e<strong>mysite\/settings.py<\/strong>\u4e2d\u6570\u636e\u5e93\u7684\u8bbe\u5b9a\uff0c\u521b\u5efa<strong>INSTALLED_APPS<\/strong>\u4e2d\u6240\u5fc5\u987b\u7684\u6570\u636e\u8868\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u6a21\u578b<\/h2>\n\n\n\n<p>\u5728poll\u5e94\u7528\u4e2d\u9700\u8981\u5efa\u7acb\u4e24\u4e2a\u6a21\u578b\uff1a<strong>Question<\/strong>\u548c<strong>Choice<\/strong>\u3002<strong>Question<\/strong>\u4e2d\u5305\u542b\u4e86question\u548cpublication date\u3002<strong>Choice<\/strong>\u6709\u4e24\u4e2a\u5b57\u6bb5\uff1atext\u578b\u7684choice\u548cvote\u3002\u6bcf\u4e2a<strong>Choice<\/strong>\u90fd\u5173\u8054\u4e00\u4e2a<strong>Question<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.db import models\n<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">from django.utils import timezone\nimport datetime<\/mark><\/strong>\n\nclass Question(models.Model):\n    question_text = models.CharField(max_length=200)\n    pub_date = models.DateTimeField(\"date published\")\n  <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">  def was_published_recently(self):\n        return self.pub_date >= timezone.now() -datetime.timedelta(days=1)<\/mark><\/strong>\n\nclass Choice(models.Model):\n    question = models.ForeignKey(Question, on_delete=models.CASCADE)\n    choice_text = models.CharField(max_length=200)\n    votes = models.IntegerField(default=0)<\/code><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">PS:\u7ea2\u8272\u90e8\u5206\u5728\u8fd9\u4e2a\u6559\u7a0b\u7684\u7b2c5\u90e8\u5206\u4f1a\u7528\u5230<\/mark><\/strong><\/p>\n\n\n\n<p>\u8fd9\u91cc\u7684\u6bcf\u4e2a\u6a21\u578b\u90fd\u662f\u7ee7\u627f<strong>django.db.models.Model<\/strong>\u7c7b\u7684\u5b50\u7c7b\u3002\u6bcf\u4e2a\u6a21\u578b\u90fd\u6709\u5f88\u591a\u7684\u7c7b\u53d8\u91cf\uff0c\u5b83\u4eec\u7528\u6765\u63cf\u8ff0\u6a21\u578b\u4e2d\u7684\u6570\u636e\u5e93\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u662f<strong>Field<\/strong>\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5982\uff1a<strong>CharField<\/strong>\u662f\u5b57\u7b26\u578b\u5b57\u6bb5\uff0c<strong>DateTimeField<\/strong>\u662fdatetime\u578b\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a<strong>Field<\/strong>\u5b9e\u4f8b\u7684\u540d\u5b57\u5c31\u662f\u6570\u636e\u8868\u7684\u5b57\u6bb5\u540d\uff0c\u5982\uff1aquestion_text\u6216pub_data\u3002<\/p>\n\n\n\n<p><strong>Field<\/strong>\u7684\u7b2c\u4e00\u4e2a\u53ef\u9009\u7684\u4f4d\u7f6e\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u4eba\u7c7b\u53ef\u8bfb\u7684\u540d\u5b57\uff0c\u5b83\u7528\u4e8eDjango\u5185\u90e8\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6587\u6863\u4f5c\u7528\u3002\u5982\u679c\u6ca1\u63d0\u4f9b\u7684\u8bddDjango\u4f1a\u4f7f\u7528\u4e00\u4e2a\u673a\u5668\u53ef\u8bfb\u7684\u540d\u5b57\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u53ea\u4e3a<strong>Question.pub_date<\/strong>\u8bbe\u7f6e\u4e86\u4eba\u7c7b\u53ef\u8bfb\u540d\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b<strong>Field<\/strong>\u7c7b\u9700\u8981\u9650\u5b9a\u53c2\u6570\u3002\u5982<strong>CharField<\/strong>\u7528<strong>max_length<\/strong>\u9650\u5b9a\u5b57\u7b26\u957f\u5ea6\u3002\u8fd8\u6709\u4e00\u4e9b\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c<strong>default<\/strong>\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u6709\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5173\u8054\uff0c\u4f7f\u7528ForeignKey\u3002\u5b83\u544a\u8bc9Django\u6bcf\u4e2a<strong>Choice<\/strong>\u90fd\u88ab\u5173\u8054\u5230\u4e00\u4e2a<strong>Question<\/strong>\u4e0a\u3002django\u652f\u6301\u6240\u6709\u5e38\u7528\u7684\u6570\u636e\u5e93\u5173\u7cfb\uff1a\u591a\u5bf9\u4e00\u3001\u591a\u5bf9\u591a\u548c\u4e00\u5bf9\u4e00\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6fc0\u6d3b\u6a21\u578b<\/h2>\n\n\n\n<p>\u5728\u6fc0\u6d3b\u6a21\u578b\u524d\u5148\u8ba9django\u77e5\u9053<strong>polls<\/strong>\u5e94\u7528\u5df2\u7ecf\u5b89\u88c5\u4e86\u3002<\/p>\n\n\n\n<p><strong>polls\/apps.py<\/strong>\u6587\u4ef6\u4e2d\u7684<strong>PollsConfig<\/strong>\u7c7b\uff0c\u5b83\u7684python\u5305\u542b\u8def\u5f84\u662f&#8217;<strong>polls.apps.PollsConfig<\/strong>\u2019\u3002\u7f16\u8f91<strong>mysite\/settings.py<\/strong>\u5c06\u5b83\u6dfb\u52a0\u5230<strong>INSTALLED_APPS<\/strong>\u4e2d\u3002\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSTALLED_APPS = &#91;\n    \"polls.apps.PollsConfig\",\n    \"django.contrib.admin\",\n    \"django.contrib.auth\",\n    \"django.contrib.contenttypes\",\n    \"django.contrib.sessions\",\n    \"django.contrib.messages\",\n    \"django.contrib.staticfiles\",\n]<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728django\u5df2\u7ecf\u77e5\u9053polls\u5e94\u7528\u4e86\u3002\u8fd0\u884c\u53e6\u4e00\u4e2a\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">python manage.py makemigrations polls<\/mark><\/code><\/pre>\n\n\n\n<p>\u4f1a\u770b\u5230\u7c7b\u4f3c\u7684\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Migrations for 'polls':\n  polls\/migrations\/0001_initial.py\n    - Create model Question\n    - Create model Choice<\/pre>\n\n\n\n<p>\u901a\u8fc7\u8fd0\u884c<strong>makemigrations<\/strong>\u544a\u8bc9django\u6a21\u578b\u6709\u4e86\u6539\u53d8\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6539\u53d8\u5b58\u50a8\u4e3a<em>migration<\/em>\u3002<\/p>\n\n\n\n<p>migration\u5b58\u50a8\u4e86\u5982\u4f55\u6539\u53d8\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b<strong>polls\/migrations\/0001_initial.py<\/strong>\u6587\u4ef6\uff0c\u5f97\u77e5\u505a\u4e86\u54ea\u4e9b\u5177\u4f53\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c31\u662f\u8bbe\u8ba1\u7528\u4e8e\u4eba\u7c7b\u53ef\u8bfb\u7684\uff0c\u53ef\u7528\u4e8e\u624b\u52a8\u5fae\u8c03\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u4f7f\u7528migrate\u5728\u6570\u636e\u5e93\u4e2d\u5efa\u7acb\u8fd9\u4e9b\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">python manage.py migrate<\/mark><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528django shell API<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">python manage.py shell<\/mark><\/code><\/pre>\n\n\n\n<p>\u6ca1\u76f4\u63a5\u4f7f\u7528python\u7684\u539f\u56e0\u662f<strong>manage.py<\/strong>\u8bbe\u7f6e\u4e86<strong>DJANGO_SETTINGS_MODULE<\/strong>\u73af\u5883\u53d8\u91cf\uff0c\u8fd9\u5c06\u4e3adjango\u63d0\u4f9b\u5bfc\u5165<strong>mysite\/settings.py<\/strong>\u7684\u8def\u5f84\u3002\u4e4b\u540e\u5c31\u53ef\u4ee5\u5bfc\u5165django\u4e2d\u7684\u6a21\u578b\u8fdb\u884c\u63a7\u5236\u53f0\u7684\u624b\u52a8\u64cd\u4f5c\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from polls.modes import Choice, Question\nQuestion.objects.all()\nfrom django.utils import timezone()\nq=Question(question_text=\"What's new?\", pub_date=timezone.now())\nq.save()\nq.id\nq.question_text\nq.pub_date\nq.question_text = \"What' up?\"\nq.save()\nQuestion.objects.all()\n\u7b49\u7b49\u64cd\u4f5c\u5427\uff01<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ecb\u7ecd\u4e00\u4e0bDjango\u7684\u7ba1\u7406<\/h2>\n\n\n\n<p>\u5efa\u7acb\u7ba1\u7406\u5458\u8d26\u53f7<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">python manage.py createsuperuser<\/mark><\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u586b\u5199\u7ba1\u7406\u5458\u8d26\u53f7\u4fe1\u606f\uff0c\u4e4b\u540e\u5c31\u53ef\u4ee5\u8fd0\u884c\u670d\u52a1\u5668\u901a\u8fc7http:\/\/localhost:8000\/admin \u6765\u767b\u9646\u7ba1\u7406\u4e86\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u73b0\u5728\u767b\u5f55\u8fdb\u53bb\u8fd8\u770b\u4e0d\u5230polls\u5e94\u7528\u548c\u65b0\u5efa\u7684\u6a21\u578b\uff0c\u6253\u5f00<strong>polls\/admin.py<\/strong>\u6587\u4ef6\uff0c\u505a\u5982\u4e0b\u7f16\u8f91<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib import admin\n\nfrom .models import Question \n\nadmin.site.register(Question)<\/pre>\n\n\n\n<p>\u8fd9\u65f6\u518d\u5237\u65b0\u9875\u9762\u5c31\u53ef\u4ee5\u770b\u5230polls\u5e94\u7528\u548cQuestion\u6a21\u578b\u4e86\uff0c\u73b0\u5728\u5e94\u8be5\u770b\u5230\u4f7f\u7528admin\u548cshell\u7ba1\u7406\u7684\u5dee\u522b\u4e86\u5427\uff01\u5404\u6709\u4f18\u70b9\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u7f6e\u6570\u636e\u5e93 \u6253\u5f00mysite\/settings.py\uff0c\u5728\u8be5\u6587\u4ef6\u4e2d\u63cf\u8ff0\u4e86django\u7684\u8bbe\u7f6e\u3002django\u9ed8\u8ba4\u4f7f\u7528\u7684\u662fSQLite\uff0c\u5982\u679c\u53ea\u662f\u4e3a\u4e86\u5b66\u4e60django\uff0c\u8fd9&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7403\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-7403","post","type-post","status-publish","format-standard","hentry","category-django"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7403"}],"version-history":[{"count":4,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7403\/revisions"}],"predecessor-version":[{"id":7432,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7403\/revisions\/7432"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}