{"id":7422,"date":"2023-04-17T23:50:20","date_gmt":"2023-04-17T15:50:20","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7422"},"modified":"2023-04-17T23:50:20","modified_gmt":"2023-04-17T15:50:20","slug":"%e7%ac%ac%e4%b8%80%e4%b8%aadjango%e5%ba%94%e7%94%a8part5","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7422","title":{"rendered":"\u7b2c\u4e00\u4e2aDjango\u5e94\u7528[part5]"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ecb\u7ecd\u81ea\u52a8\u6d4b\u8bd5<\/h2>\n\n\n\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u81ea\u52a8\u6d4b\u8bd5\uff1f<\/strong><\/p>\n\n\n\n<p>\u5c31\u662f\u8bbe\u7f6e\u597d\u4e00\u5957\u6d4b\u8bd5\u6761\u4ef6\uff0c\u5f53\u5bf9\u5e94\u7528\u505a\u4e86\u8c03\u6574\u540e\uff0c\u53ef\u4ee5\u81ea\u52a8\u68c0\u67e5\u529f\u80fd\u662f\u4e0d\u662f\u8fd8\u80fd\u6ee1\u8db3\u521d\u59cb\u8bbe\u5b9a\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u7684\u8fdb\u4e00\u9010\u4e00\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u521b\u5efa\u6d4b\u8bd5\uff1f<\/strong><\/p>\n\n\n\n<p>1\u3001\u6d4b\u8bd5\u4f1a\u8282\u7701\u4f60\u7684\u65f6\u95f4<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u53ef\u4ee5\u4fdd\u8bc1\u5e94\u7528\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u5728\u590d\u6742\u7684\u5e94\u7528\u4e2d\u7ecf\u5e38\u65f6\u6570\u5341\u4e2a\u590d\u6742\u7684\u7ec4\u4ef6\u8fdb\u884c\u4ea4\u4e92\u3002\u5728\u8fd9\u4e9b\u7ec4\u4ef6\u4e2d\u505a\u4e00\u70b9\u70b9\u7684\u4fee\u6539\u90fd\u6709\u53ef\u80fd\u4ea7\u751f\u672a\u77e5\u7684\u8fde\u9501\u53cd\u5e94\u3002\u5728\u4f7f\u7528\u6570\u5341\u79cd\u4e0d\u540c\u7684\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4ee5\u4fdd\u8bc1\u5e94\u7528\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u8fd9\u4e0d\u6b63\u662f\u8282\u7701\u4e86\u65f6\u95f4\u3002\u5982\u679c\u51fa\u73b0\u4e86\u95ee\u9898\uff0c\u6d4b\u8bd5\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>2\u3001\u6d4b\u8bd5\u4e0d\u4ec5\u4ec5\u53ef\u4ee5\u6307\u51fa\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u9632\u6b62\u95ee\u9898\u53d1\u751f<\/p>\n\n\n\n<p>\u6ca1\u6709\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u5e94\u7528\u7684\u884c\u4e3a\u548c\u76ee\u7684\u8ba9\u4eba\u8ff7\u60d1\uff0c\u5373\u4f7f\u8fd9\u662f\u4f60\u81ea\u5df1\u5199\u7684\u7a0b\u5e8f\uff0c\u4f60\u4e5f\u8981\u82b1\u8d39\u4e00\u4e9b\u65f6\u95f4\u53bb\u7406\u89e3\u5b83\u8981\u5e72\u4ec0\u4e48\u3002<\/p>\n\n\n\n<p>3\u3001\u6d4b\u8bd5\u4f7f\u4ee3\u7801\u66f4\u6709\u5438\u5f15\u529b<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u5f00\u53d1\u51fa\u4e86\u4e00\u4e2a\u5f88\u68d2\u7684\u8f6f\u4ef6\uff0c\u4f46\u4f60\u4f1a\u53d1\u73b0\u8bb8\u591a\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u62d2\u7edd\u67e5\u770b\u5b83\uff0c\u56e0\u4e3a\u5b83\u7f3a\u4e4f\u6d4b\u8bd5;\u6ca1\u6709\u6d4b\u8bd5\uff0c\u4ed6\u4eec\u4e0d\u4f1a\u76f8\u4fe1\u5b83\u3002\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5728\u8ba4\u771f\u5bf9\u5f85\u8f6f\u4ef6\u4e4b\u524d\u5e0c\u671b\u770b\u5230\u8f6f\u4ef6\u4e2d\u7684\u6d4b\u8bd5\uff0c\u8fd9\u4e5f\u662f\u60a8\u5f00\u59cb\u7f16\u5199\u6d4b\u8bd5\u7684\u53e6\u4e00\u4e2a\u539f\u56e0\u3002<\/p>\n\n\n\n<p>4\u3001\u6d4b\u8bd5\u53ef\u4ee5\u8ba9\u56e2\u961f\u534f\u4f5c<\/p>\n\n\n\n<p>\u524d\u9762\u7684\u51e0\u70b9\u662f\u4ece\u7ef4\u62a4\u5e94\u7528\u7a0b\u5e8f\u7684\u5355\u4e2a\u5f00\u53d1\u4eba\u5458\u7684\u89d2\u5ea6\u51fa\u53d1\u7684\u3002\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u7531\u56e2\u961f\u7ef4\u62a4\u3002\u6d4b\u8bd5\u53ef\u4ee5\u4fdd\u8bc1\u540c\u4e8b\u4e0d\u4f1a\u65e0\u610f\u4e2d\u7834\u574f\u4f60\u7684\u4ee3\u7801(\u4f60\u4e5f\u4e0d\u4f1a\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u7834\u574f\u4ed6\u4eec\u7684\u4ee3\u7801)\u3002\u5982\u679c\u4f60\u60f3\u6210\u4e3aDjango\u7a0b\u5e8f\u5458\uff0c\u5c31\u5fc5\u987b\u64c5\u957f\u7f16\u5199\u6d4b\u8bd5!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u6d4b\u8bd5\u7b56\u7565<\/h2>\n\n\n\n<p>\u6709\u4e9b\u7a0b\u5e8f\u5458\u9075\u5faa\u201c\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u201d(test-driven development)\u7684\u539f\u5219;\u4ed6\u4eec\u5b9e\u9645\u4e0a\u662f\u5728\u5199\u4ee3\u7801\u4e4b\u524d\u5199\u6d4b\u8bd5\u3002\u8fd9\u53ef\u80fd\u770b\u8d77\u6765\u8fdd\u53cd\u76f4\u89c9\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u7c7b\u4f3c\u4e8e\u5927\u591a\u6570\u4eba\u7ecf\u5e38\u505a\u7684\u4e8b\u60c5:\u4ed6\u4eec\u63cf\u8ff0\u4e00\u4e2a\u95ee\u9898\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e9b\u4ee3\u7801\u6765\u89e3\u51b3\u5b83\u3002\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u7528Python\u6d4b\u8bd5\u7528\u4f8b\u5c06\u95ee\u9898\u5f62\u5f0f\u5316\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5199\u7b2c\u4e00\u4e2a\u6d4b\u8bd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py shell\n&gt;&gt;&gt; import datetime\n&gt;&gt;&gt; from django.utils import timezone\n&gt;&gt;&gt; from polls.models import Question\n&gt;&gt;&gt; # create a Question instance with pub_date 30 days in the future\n&gt;&gt;&gt; future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))\n&gt;&gt;&gt; # was it published recently?\n&gt;&gt;&gt; future_question.was_published_recently()\nTrue<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5efa\u7acb\u4e00\u4e2a\u6d4b\u8bd5\u66b4\u9732\u95ee\u9898<\/h2>\n\n\n\n<p>\u901a\u5e38\uff0c\u6d4b\u8bd5\u653e\u5728\u5e94\u7528\u7a0b\u5e8f\u7684tests.py\u6587\u4ef6\u4e2d;\u6d4b\u8bd5\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5728\u4efb\u4f55\u6587\u4ef6\u540d\u4ee5test\u5f00\u5934\u7684\u6587\u4ef6\u4e2d\u67e5\u627etests\u3002\u5199\u6d4b\u8bd5\u6587\u4ef6polls\/tests.py:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import datetime\n\nfrom django.test import TestCase\nfrom django.utils import timezone\n\nfrom .models import Question\n\n\nclass QuestionModelTests(TestCase):\n    def test_was_published_recently_with_future_question(self):\n        \"\"\"\n        was_published_recently() returns False for questions whose pub_date\n        is in the future.\n        \"\"\"\n        time = timezone.now() + datetime.timedelta(days=30)\n        future_question = Question(pub_date=time)\n        self.assertIs(future_question.was_published_recently(), False)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u521b\u5efa\u4e86\u4e00\u4e2a<strong>django.test.TestCase<\/strong>\u5b50\u7c7b\uff0c\u5176\u5305\u542b\u4e86\u4e00\u4e2a<strong>pub_date<\/strong>\u65e5\u671f\u5728\u672a\u6765\u7684<strong>Question<\/strong>\u7684\u5b9e\u4f8b\u3002\u6211\u4eec\u68c0\u67e5<strong>was_published_recently()<\/strong>\u7684\u8f93\u51fa\u2014\u2014\u5b83\u4f1a\u8f93\u51fa<strong>False<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fd0\u884c\u6d4b\u8bd5<\/h2>\n\n\n\n<p>\u5728\u7ec8\u7aef\u91cc\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python manage.py test polls<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u4f1a\u770b\u5230\u7c7b\u4f3c\u7684\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Found 1 test(s).                                                                                                                               \r\nCreating test database for alias 'default'...\r\nSystem check identified no issues (0 silenced).\r\nF\r\n======================================================================\r\nF\r\n======================================================================\r\nFAIL: test_was_published_recently_with_futuren_question (polls.tests.QuestionModelTests)\r\n----------------------------------------------------------------------\r\nTraceback (most recent call last):\r\n  File \"E:\\practices\\python\\Django_test\\mysite\\polls\\tests.py\", line 10, in test_was_published_recently_with_futuren_question\r\n    self.assertIs(future_question.was_published_recently(),False)\r\nAssertionError: True is not False\r\n\r\n----------------------------------------------------------------------\r\nRan 1 test in 0.002s\r\n\r\nFAILED (failures=1)\r\nDestroying test database for alias 'default'...\r<\/pre>\n\n\n\n<p>\u5176\u95f4\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>manage.py test polls<\/strong>\u5728<strong>polls<\/strong>\u5e94\u7528\u4e2d\u67e5\u627e\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li>\u5b83\u627e\u5230\u4e00\u4e2a<strong>django.test.TestCase<\/strong>\u7c7b\u7684\u5b50\u7c7b\u3002<\/li>\n\n\n\n<li>\u5b83\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6570\u636e\u5e93\u7528\u4e8e\u6d4b\u8bd5\u76ee\u7684\u3002<\/li>\n\n\n\n<li>\u67e5\u627e\u6d4b\u8bd5\u65b9\u6cd5\u2014\u2014\u4ee5<strong>test<\/strong>\u5f00\u5934\u547d\u540d\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5728test_was_published_recently_with_future_question\u4e2d\u5b83\u5efa\u7acb\u4e00\u4e2aQuestion\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u7684<strong>pub_date<\/strong>\u5b57\u6bb5\u662f30\u5929\u540e\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528<strong>assertIs()<\/strong>\u65b9\u6cd5\uff0c\u5b83\u53d1\u73b0<strong>was_published_recently()<\/strong>\u8fd4\u56de <strong>True<\/strong>\uff0c\u867d\u7136\u6211\u4eec\u5e0c\u671b\u5b83\u8fd4\u56de<strong>False<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fee\u590d\u95ee\u9898<\/h2>\n\n\n\n<p>\u73b0\u5728\u5df2\u7ecf\u77e5\u9053\u95ee\u9898\u53d1\u751f\u5728Question.was_published_recently()\uff0c\u5f53pub_date\u5b57\u6bb5\u7684\u65e5\u671f\u5728\u672a\u6765\u65f6\u5e94\u8be5\u8fd4\u56deFalse\u3002\u4fee\u6539models.py\u4e2d\u7684\u65b9\u6cd5\u3002\u6240\u4ee5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def was_published_recently(self):\r\n    now = timezone.now()\r\n    return now - datetime.timedelta(days=1) &lt;= self.pub_date &lt;= now<\/code><\/pre>\n\n\n\n<p>\u5728\u672a\u6765\uff0c\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u51fa\u73b0\u8bb8\u591a\u5176\u4ed6\u95ee\u9898\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u80af\u5b9a\u4e0d\u4f1a\u65e0\u610f\u4e2d\u518d\u6b21\u5f15\u5165\u8fd9\u4e2a\u9519\u8bef\uff0c\u56e0\u4e3a\u8fd0\u884c\u6d4b\u8bd5\u4f1a\u7acb\u5373\u53d1\u51fa\u8b66\u544a\u3002\u6211\u4eec\u53ef\u4ee5\u8ba4\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd9\u4e00\u5c0f\u90e8\u5206\u6c38\u8fdc\u662f\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u66f4\u5168\u9762\u7684\u6d4b\u8bd5<\/h2>\n\n\n\n<p>\u5b9e\u9645\u4e0a\u5728\u4fee\u590d\u4e00\u4e2aBUG\u7684\u540c\u65f6\u5f88\u6709\u53ef\u80fd\u4f1a\u5efa\u7acb\u53e6\u4e00\u4e2aBUG\u3002\u6dfb\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u65b9\u6cd5\u5230\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4e3a\u4e86\u66f4\u5168\u9762\u7684\u6d4b\u8bd5\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>def test_was_published_recently_with_old_question(self):<br>&#8220;&#8221;&#8221;<br>was_published_recently() returns False for questions whose pub_date<br>is older than 1 day.<br>&#8220;&#8221;&#8221;<br>time = timezone.now() &#8211; datetime.timedelta(days=1, seconds=1)<br>old_question = Question(pub_date=time)<br>self.assertIs(old_question.was_published_recently(), False)<\/p>\n\n\n\n<p>def test_was_published_recently_with_recent_question(self):<br>&#8220;&#8221;&#8221;<br>was_published_recently() returns True for questions whose pub_date<br>is within the last day.<br>&#8220;&#8221;&#8221;<br>time = timezone.now() &#8211; datetime.timedelta(hours=23, minutes=59, seconds=59)<br>recent_question = Question(pub_date=time)<br>self.assertIs(recent_question.was_published_recently(), True)<\/p>\n\n\n\n<p>\u73b0\u5728\u5bf9\u4e8eQuestion\u6a21\u578b\u65b9\u6cd5\u5df2\u7ecf\u6709\u4e09\u6761\u6d4b\u8bd5\u65b9\u6848\u4e86\uff0c\u968f\u7740\u5e94\u7528\u529f\u80fd\u7684\u589e\u52a0\u4f1a\u9010\u6e10\u52a0\u5165\u65b0\u7684\u6d4b\u8bd5\u65b9\u6848\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u8bd5\u89c6\u56fe<\/h2>\n\n\n\n<p>\u5728\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u5bc6\u5207\u5173\u6ce8\u4ee3\u7801\u7684\u5185\u90e8\u884c\u4e3a\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u6d4b\u8bd5\uff0c\u6211\u4eec\u5e0c\u671b\u68c0\u67e5\u5b83\u7684\u884c\u4e3a\uff0c\u5c31\u50cf\u7528\u6237\u901a\u8fc7web\u6d4f\u89c8\u5668\u4f53\u9a8c\u5230\u5b83\u4e00\u6837\u3002\u5728\u6211\u4eec\u5c1d\u8bd5\u89e3\u51b3\u4efb\u4f55\u95ee\u9898\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Django\u6d4b\u8bd5\u5ba2\u6237\u7aef<\/h2>\n\n\n\n<p>Django\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4eff\u771f\u7a0b\u5e8fClient\u7528\u4e8e\u6a21\u62df\u7528\u6237\u5728\u89c6\u56fe\u5c42\u7684\u4ea4\u4e92\u5f0f\u8bbf\u95ee\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728tests.py\u4e2d\uff0c\u751a\u81f3\u5728shell\u4e2d\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u518d\u6b21\u4eceshell\u5f00\u59cb\uff0c\u5728\u90a3\u91cc\u6211\u4eec\u9700\u8981\u505a\u4e00\u4e9b\u5728tests.py\u4e2d\u4e0d\u662f\u5fc5\u987b\u505a\u7684\u4e8b\u60c5\u2014\u2014\u8bbe\u7f6e\u6d4b\u8bd5\u73af\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python manage.py shell\n>>> from django.test.utils import setup_test_environment\r\n>>> setup_test_environment()<\/pre>\n\n\n\n<p>setup_test_environment()\u5b89\u88c5\u4e00\u4e2a\u6a21\u7248\u6e32\u67d3\u5668\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u68c0\u67e5\u54cd\u5e94\u4e0a\u7684\u4e00\u4e9b\u989d\u5916\u5c5e\u6027\uff0c\u5982\uff1aresponse.context\uff0c\u5982\u679c\u6ca1\u6709\u6a21\u7248\u6e32\u67d3\u5668\uff0c\u5c31\u6ca1\u529e\u6cd5\u505a\u8fd9\u4e9b\u68c0\u67e5\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">\u6ce8\u610f<\/mark><\/strong>\uff0c<em>\u8fd9\u4e2a\u65b9\u6cd5\u5e76\u4e0d\u8bbe\u7f6e\u6d4b\u8bd5\u6570\u636e\u5e93<\/em>\uff0c\u6240\u4ee5\u63a5\u4e0b\u6765\u7684\u64cd\u4f5c\u4f9d\u8d56\u4e8e\u73b0\u5b58\u7684\u6570\u636e\u5e93\uff0c\u5e76\u4e14\u8f93\u51fa\u4f1a\u4f9d\u8d56\u4e8e<strong>Question<\/strong>\u5e93\u4e2d\u5df2\u7ecf\u5efa\u7acb\u7684\u8bb0\u5f55\u3002\u5982\u679csettings.py\u4e2d\u7684TIME_ZONE\u6ca1\u6709\u8bbe\u7f6e\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u6709\u672a\u77e5\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u5bfc\u5165test client\u7c7b\uff08\u5728\u4e4b\u540e\u7684tests.py\u5c06\u4f7f\u7528django.test.TestCase\u7c7b\uff0c\u5b83\u6709\u81ea\u5df1\u7684\u89c6\u56fe\u5c42\u5ba2\u6237\u7aef\uff0c\u6240\u4ee5\u8fd9\u91cc\u5e76\u4e0d\u9700\u8981\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> from django.test import Client\r\n>>> # create an instance of the client for our use\r\n>>> client = Client()<\/pre>\n\n\n\n<p>\u73b0\u5728\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\uff0c\u53ef\u4ee5\u8ba9client\u4e3a\u6211\u4eec\u505a\u4e00\u4e9b\u4e8b\u60c5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> # get a response from '\/'\r\n>>> response = client.get(\"\/\")\r\nNot Found: \/\r\n>>> # we should expect a 404 from that address; if you instead see an\r\n>>> # \"Invalid HTTP_HOST header\" error and a 400 response, you probably\r\n>>> # omitted the setup_test_environment() call described earlier.\r\n>>> response.status_code\r\n404\r\n>>> # on the other hand we should expect to find something at '\/polls\/'\r\n>>> # we'll use 'reverse()' rather than a hardcoded URL\r\n>>> from django.urls import reverse\r\n>>> response = client.get(reverse(\"polls:index\"))\r\n>>> response.status_code\r\n200\r\n>>> response.content\r\nb'\\n    &lt;ul>\\n    \\n        &lt;li>&lt;a href=\"\/polls\/1\/\">What&amp;#x27;s up?&lt;\/a>&lt;\/li>\\n    \\n    &lt;\/ul>\\n\\n'\r\n>>> response.context[\"latest_question_list\"]\r\n&lt;QuerySet [&lt;Question: What's up?>]><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6539\u8fdb\u6211\u4eec\u7684\u89c6\u56fe<\/h2>\n\n\n\n<p>polls\u7684\u5217\u8868\u663e\u793a\u4e86\u5c1a\u672a\u516c\u5e03\u7684\u540d\u5355\uff0c\u6211\u4eec\u8981\u5bf9<strong>get_queryset()<\/strong>\u65b9\u6cd5\u8fdb\u884c\u6539\u8fdb\uff0c\u8ba9\u5b83\u4e5f\u5bf9\u6bd4<strong>timezone.now()<\/strong>\uff0c\u9996\u5148\u5728polls\/views.py\u6dfb\u52a0\u5934\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.utils import timezone<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u4fee\u6539<strong>get_queryset()<\/strong>\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_queryset(self):\r\n    \"\"\"\r\n    Return the last five published questions (not including those set to be\r\n    published in the future).\r\n    \"\"\"\r\n    return Question.objects.filter(pub_date__lte=timezone.now()).order_by(\"-pub_date\")&#91;\r\n        :5\r\n    ]<\/code><\/pre>\n\n\n\n<p><strong>Question.objects.filter(pub_date__lte=timezone.now())<\/strong>\u8fd4\u56de\u4e00\u4e2a\u67e5\u8be2\u96c6\uff0c\u5b83\u5305\u542b \u4e86<strong>Question<\/strong>\u8868\u4e2d<strong>pub_date<\/strong>\u5c0f\u4e8e\u7b49\u4e8e<strong>timezone.now()<\/strong>\u7684\u8bb0\u5f55\uff0c\u4e5f\u5c31\u662f\u65e9\u4e8e\u6216\u7b49\u4e8e<strong>timezone.now()<\/strong>\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u8bd5\u6211\u4eec\u7684\u89c6\u56fe<\/h2>\n\n\n\n<p>\u73b0\u5728\u53ef\u4ee5\u6253\u5f00\u6d4f\u89c8\u5668\u67e5\u770b\u6211\u4eec\u4fee\u6539\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u5982\u679c\u4e0d\u60f3\u6bcf\u6b21\u90fd\u6253\u5f00\u6d4f\u89c8\u5668\u9a8c\u8bc1\u7684\u8bdd\uff0c\u90a3\u5c31\u9700\u8981\u6211\u4eec\u7684\u6d4b\u8bd5\u7a0b\u5e8f\u4e86\uff0c\u4fee\u6539<strong>polls\/tests.py<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import reverse<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5feb\u6377\u51fd\u6570\u6765\u5efa\u7acbquestion\u8bb0\u5f55\uff0c\u8fd8\u6709\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def create_question(question_text, days):\r\n    \"\"\"\r\n    Create a question with the given `question_text` and published the\r\n    given number of `days` offset to now (negative for questions published\r\n    in the past, positive for questions that have yet to be published).\r\n    \"\"\"\r\n    time = timezone.now() + datetime.timedelta(days=days)\r\n    return Question.objects.create(question_text=question_text, pub_date=time)\r\n\r\n\r\nclass QuestionIndexViewTests(TestCase):\r\n    def test_no_questions(self):\r\n        \"\"\"\r\n        If no questions exist, an appropriate message is displayed.\r\n        \"\"\"\r\n        response = self.client.get(reverse(\"polls:index\"))\r\n        self.assertEqual(response.status_code, 200)\r\n        self.assertContains(response, \"No polls are available.\")\r\n        self.assertQuerySetEqual(response.context&#91;\"latest_question_list\"], &#91;])\r\n\r\n    def test_past_question(self):\r\n        \"\"\"\r\n        Questions with a pub_date in the past are displayed on the\r\n        index page.\r\n        \"\"\"\r\n        question = create_question(question_text=\"Past question.\", days=-30)\r\n        response = self.client.get(reverse(\"polls:index\"))\r\n        self.assertQuerySetEqual(\r\n            response.context&#91;\"latest_question_list\"],\r\n            &#91;question],\r\n        )\r\n\r\n    def test_future_question(self):\r\n        \"\"\"\r\n        Questions with a pub_date in the future aren't displayed on\r\n        the index page.\r\n        \"\"\"\r\n        create_question(question_text=\"Future question.\", days=30)\r\n        response = self.client.get(reverse(\"polls:index\"))\r\n        self.assertContains(response, \"No polls are available.\")\r\n        self.assertQuerySetEqual(response.context&#91;\"latest_question_list\"], &#91;])\r\n\r\n    def test_future_question_and_past_question(self):\r\n        \"\"\"\r\n        Even if both past and future questions exist, only past questions\r\n        are displayed.\r\n        \"\"\"\r\n        question = create_question(question_text=\"Past question.\", days=-30)\r\n        create_question(question_text=\"Future question.\", days=30)\r\n        response = self.client.get(reverse(\"polls:index\"))\r\n        self.assertQuerySetEqual(\r\n            response.context&#91;\"latest_question_list\"],\r\n            &#91;question],\r\n        )\r\n\r\n    def test_two_past_questions(self):\r\n        \"\"\"\r\n        The questions index page may display multiple questions.\r\n        \"\"\"\r\n        question1 = create_question(question_text=\"Past question 1.\", days=-30)\r\n        question2 = create_question(question_text=\"Past question 2.\", days=-5)\r\n        response = self.client.get(reverse(\"polls:index\"))\r\n        self.assertQuerySetEqual(\r\n            response.context&#91;\"latest_question_list\"],\r\n            &#91;question2, question1],\r\n        )<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u770b\u4e00\u4e0b\u4e0a\u9762\u7684\u6d4b\u8bd5\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\u662fquestion\u7684\u5feb\u6377\u51fd\u6570\uff1a<strong>create_question<\/strong>\uff0c\u7528\u4e8e\u5728\u521b\u5efaquestion\u65f6\u51cf\u5c11\u4e00\u4e9b\u91cd\u590d\u3002<\/p>\n\n\n\n<p><strong>test_no_questions<\/strong>\u5e76\u4e0d\u521b\u5efa\u4efb\u4f55question\uff0c\u4f46\u662f\u68c0\u67e5\u6d88\u606f\u201cNo polls are available.\u201d\u5e76\u9a8c\u8bc1<strong>latest_question_list<\/strong>\u662f\u7a7a\u3002\u6ce8\u610f\uff1a<strong>django.test.TestCase<\/strong>\u7c7b\u63d0\u4f9b\u989d\u5916\u7684\u65ad\u8a00\u65b9\u6cd5\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86<strong>assertContains()<\/strong>\u548c<strong>assertQuerySetEqual()<\/strong>.<\/p>\n\n\n\n<p>\u5728<strong>test_past_question<\/strong>\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aquestion\u5e76\u9a8c\u8bc1\u5b83\u662f\u5426\u51fa\u73b0\u5728\u5217\u8868\u4e2d\u3002<\/p>\n\n\n\n<p>\u5728<strong>test_future_question<\/strong>\u4e2d\uff0c\u4ee5\u672a\u6765\u65f6\u95f4\u521b\u5efa\u4e00\u4e2aquestion\u3002\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u90fd\u4f1a\u91cd\u7f6e\u6570\u636e\u5e93\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u95ee\u9898\u4e0d\u518d\u5b58\u5728\uff0c\u56e0\u6b64\u7d22\u5f15\u4e2d\u4e5f\u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u7528\u8fd9\u4e9b\u6d4b\u8bd5\u6765\u8bb2\u8ff0\u4e00\u4e2a\u7ba1\u7406\u5458\u8f93\u5165\u548c\u7f51\u7ad9\u7528\u6237\u4f53\u9a8c\u7684\u6545\u4e8b\uff0c\u5e76\u68c0\u67e5\u5728\u7cfb\u7edf\u72b6\u6001\u7684\u6bcf\u4e00\u4e2a\u65b0\u53d8\u5316\u65f6\uff0c\u662f\u5426\u53d1\u5e03\u4e86\u9884\u671f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u8bd5DetailView<\/h2>\n\n\n\n<p>\u4e4b\u524d\u7684\u5de5\u4f5c\u6ca1\u4ec0\u4e48\u95ee\u9898;\u7136\u800c\uff0c\u5373\u4f7f\u672a\u6765\u7684question\u4e0d\u51fa\u73b0\u5728\u7d22\u5f15\u4e2d\uff0c\u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\u5b83\u4eec\uff0c\u53ea\u8981\u4ed6\u4eec\u77e5\u9053\u6216\u731c\u5bf9\u4e86URL\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u7ed9DetailView\u6dfb\u52a0\u4e00\u4e2a\u7c7b\u4f3c\u7684\u7ea6\u675f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>polls\/views.py\nclass DetailView(generic.DetailView):\r\n    ...\r\n\r\n    def get_queryset(self):\r\n        \"\"\"\r\n        Excludes any questions that aren't published yet.\r\n        \"\"\"\r\n        return Question.objects.filter(pub_date__lte=timezone.now())<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5e94\u8be5\u6dfb\u52a0\u4e00\u4e9b\u6d4b\u8bd5\uff0c\u4ee5\u68c0\u67e5\u53ef\u4ee5\u663e\u793apub_date\u4e3a\u8fc7\u53bb\u7684<strong>Question<\/strong>\uff0c\u800c\u4e0d\u80fd\u663e\u793apub_date\u4e3a\u672a\u6765\u7684<strong>Question<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>poll\/tests.py\nclass QuestionDetailViewTests(TestCase):\r\n    def test_future_question(self):\r\n        \"\"\"\r\n        The detail view of a question with a pub_date in the future\r\n        returns a 404 not found.\r\n        \"\"\"\r\n        future_question = create_question(question_text=\"Future question.\", days=5)\r\n        url = reverse(\"polls:detail\", args=(future_question.id,))\r\n        response = self.client.get(url)\r\n        self.assertEqual(response.status_code, 404)\r\n\r\n    def test_past_question(self):\r\n        \"\"\"\r\n        The detail view of a question with a pub_date in the past\r\n        displays the question's text.\r\n        \"\"\"\r\n        past_question = create_question(question_text=\"Past Question.\", days=-5)\r\n        url = reverse(\"polls:detail\", args=(past_question.id,))\r\n        response = self.client.get(url)\r\n        self.assertContains(response, past_question.question_text)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd\u81ea\u52a8\u6d4b\u8bd5 \u4e00\u3001\u4ec0\u4e48\u662f\u81ea\u52a8\u6d4b\u8bd5\uff1f \u5c31\u662f\u8bbe\u7f6e\u597d\u4e00\u5957\u6d4b\u8bd5\u6761\u4ef6\uff0c\u5f53\u5bf9\u5e94\u7528\u505a\u4e86\u8c03\u6574\u540e\uff0c\u53ef\u4ee5\u81ea\u52a8\u68c0\u67e5\u529f\u80fd\u662f\u4e0d\u662f\u8fd8\u80fd\u6ee1\u8db3\u521d\u59cb\u8bbe\u5b9a\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u7684\u8fdb\u4e00\u9010\u4e00\u6d4b\u8bd5\u3002 \u4e8c\u3001\u4e3a\u4ec0\u4e48&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7422\"> 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,15],"tags":[],"class_list":["post-7422","post","type-post","status-publish","format-standard","hentry","category-django","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7422","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=7422"}],"version-history":[{"count":9,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7422\/revisions"}],"predecessor-version":[{"id":7436,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7422\/revisions\/7436"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}