{"id":288,"date":"2021-01-17T20:33:36","date_gmt":"2021-01-17T14:33:36","guid":{"rendered":"http:\/\/www.jichangqing.net\/wordpress\/?p=288"},"modified":"2021-01-17T21:59:56","modified_gmt":"2021-01-17T15:59:56","slug":"django-3-by-example%e5%ad%a6%e4%b9%a0-%e7%ac%ac%e4%b8%89%e7%ab%a0-%e5%bb%ba%e6%90%9c%e7%b4%a2%e5%bc%95%e6%93%8e","status":"publish","type":"post","link":"http:\/\/www.jichangqing.net\/wordpress\/?p=288","title":{"rendered":"Django 3 by example\u5b66\u4e60 \u7b2c\u4e09\u7ae0 \u5efa\u641c\u7d22\u5f15\u64ce"},"content":{"rendered":"<p>\u5b89\u88c5\u89c1\u8fd9\u91cc\uff1a<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"0Z9DolnIoU\"><p><a href=\"http:\/\/www.jichangqing.net\/wordpress\/?p=274\">Windows\u4e0bPostgreSQL\u5efa\u65b0\u7528\u6237\u548c\u5e93\u3002<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"\u300a Windows\u4e0bPostgreSQL\u5efa\u65b0\u7528\u6237\u548c\u5e93\u3002 \u300b\u2014\u68a6\u7fd4\u5929\u7a7a\" src=\"http:\/\/www.jichangqing.net\/wordpress\/?p=274&#038;embed=true#?secret=vvorAEStkM#?secret=0Z9DolnIoU\" data-secret=\"0Z9DolnIoU\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>setting.py\u4e2d\u6570\u636e\u5e93\u8bbe<\/p>\n<pre>DATABASES <span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #63a35c;\">{\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'default'<\/span><span style=\"color: #a71d5d;\">: <\/span><span style=\"color: #63a35c;\">{\r\n<\/span><span style=\"color: #969896;\">#'ENGINE': 'django.db.backends.sqlite3',\r\n<\/span><span style=\"color: #969896;\">        #'NAME': BASE_DIR \/ 'db.sqlite3',\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'ENGINE'<\/span><span style=\"color: #a71d5d;\">: <\/span><span style=\"font-weight: bold; color: #008080;\">'django.db.backends.postgresql'<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'NAME'<\/span><span style=\"color: #a71d5d;\">: <\/span><span style=\"font-weight: bold; color: #008080;\">'blog'<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'USER'<\/span><span style=\"color: #a71d5d;\">: <\/span><span style=\"font-weight: bold; color: #008080;\">'blog'<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'PASSWORD'<\/span><span style=\"color: #a71d5d;\">: <\/span><span style=\"font-weight: bold; color: #008080;\">'password'<\/span><span style=\"color: #63a35c;\">, #\u5efa\u8868\u65f6\u8bbe\u7f6e\u7684\u5bc6\u7801\r\n<\/span><span style=\"color: #63a35c;\">    }\r\n<\/span><span style=\"color: #63a35c;\">}<\/span><\/pre>\n<p>\u6570\u636e\u8fc1\u79fb<\/p>\n<p>python manage. py migrate<\/p>\n<p>\u521b\u5efaadmin\u7528\u6237<\/p>\n<p>python manage. py createsuperuser<\/p>\n<pre>INSTALLED_APPS <span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #63a35c;\">[\r\n<\/span><span style=\"color: #63a35c;\">#...\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'django.contrib.postgres'<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"color: #63a35c;\">#...\r\n<\/span><span style=\"color: #63a35c;\">]<\/span><\/pre>\n<p>\u521b\u5efaadmin\u7528\u6237<\/p>\n<p>forms.py<\/p>\n<pre><span style=\"color: #a71d5d;\">class <\/span>SearchForm<span style=\"color: #63a35c;\">(<\/span>forms<span style=\"color: #63a35c;\">.<\/span>Form<span style=\"color: #63a35c;\">)<\/span><span style=\"color: #a71d5d;\">:\r\n<\/span>query <span style=\"color: #a71d5d;\">= <\/span>forms<span style=\"color: #63a35c;\">.<\/span><span style=\"color: #0086b3;\">CharField<\/span><span style=\"color: #63a35c;\">()<\/span><\/pre>\n<p>views.py<\/p>\n<pre><span style=\"color: #a71d5d;\">from <\/span><span style=\"color: #63a35c;\">.<\/span>forms <span style=\"color: #a71d5d;\">import <\/span>EmailPostForm<span style=\"color: #63a35c;\">, <\/span>CommentForm<span style=\"color: #63a35c;\">, <\/span>SearchForm\r\n<span style=\"color: #a71d5d;\">from <\/span>django<span style=\"color: #63a35c;\">.<\/span>contrib<span style=\"color: #63a35c;\">.<\/span>postgres<span style=\"color: #63a35c;\">.<\/span>search <span style=\"color: #a71d5d;\">import <\/span>SearchVector<span style=\"color: #63a35c;\">, <\/span>SearchQuery<span style=\"color: #63a35c;\">, <\/span>SearchRank<span style=\"color: #63a35c;\">, <\/span>TrigramSimilarity<\/pre>\n<pre><span style=\"color: #a71d5d;\">def <\/span><span style=\"color: #795da3;\">post_search<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"color: #0086b3;\">request<\/span><span style=\"color: #63a35c;\">)<\/span><span style=\"color: #a71d5d;\">:\r\n<\/span>form <span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #0086b3;\">SearchForm<\/span><span style=\"color: #63a35c;\">()\r\n<\/span>query <span style=\"color: #a71d5d;\">= None\r\n<\/span>results <span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #63a35c;\">[]\r\n<\/span><span style=\"color: #a71d5d;\">if <\/span><span style=\"font-weight: bold; color: #008080;\">'query' <\/span><span style=\"color: #a71d5d;\">in <\/span><span style=\"color: #0086b3;\">request<\/span><span style=\"color: #63a35c;\">.<\/span>GET<span style=\"color: #a71d5d;\">:\r\n<\/span>form <span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #0086b3;\">SearchForm<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"color: #0086b3;\">request<\/span><span style=\"color: #63a35c;\">.<\/span>GET<span style=\"color: #63a35c;\">)\r\n<\/span><span style=\"color: #a71d5d;\">if <\/span>form<span style=\"color: #63a35c;\">.<\/span><span style=\"color: #0086b3;\">is_valid<\/span><span style=\"color: #63a35c;\">()<\/span><span style=\"color: #a71d5d;\">:\r\n<\/span>query <span style=\"color: #a71d5d;\">= <\/span>form<span style=\"color: #63a35c;\">.<\/span>cleaned_data<span style=\"color: #63a35c;\">[<\/span><span style=\"font-weight: bold; color: #008080;\">'query'<\/span><span style=\"color: #63a35c;\">]\r\n#\u4e00\u822c\u67e5\u8be2\r\n<\/span><span style=\"color: #969896;\"># results = Post.published.annotate(\r\n<\/span><span style=\"color: #969896;\">            #     search = SearchVector('title', 'body'),\r\n<\/span><span style=\"color: #969896;\">            # ).filter(search=query)\r\n<\/span><span style=\"color: #808080;\">search_vector <\/span><span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #0086b3;\">SearchVector<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"font-weight: bold; color: #008080;\">'title'<\/span><span style=\"color: #63a35c;\">, <\/span><span style=\"color: #660099;\">weight<\/span><span style=\"color: #a71d5d;\">=<\/span><span style=\"font-weight: bold; color: #008080;\">'A'<\/span><span style=\"color: #63a35c;\">) <\/span><span style=\"color: #a71d5d;\">+ <\/span>\\\r\n                            <span style=\"color: #0086b3;\">SearchVector<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"font-weight: bold; color: #008080;\">'body'<\/span><span style=\"color: #63a35c;\">, <\/span><span style=\"color: #660099;\">weight<\/span><span style=\"color: #a71d5d;\">=<\/span><span style=\"font-weight: bold; color: #008080;\">'B'<\/span><span style=\"color: #63a35c;\">)\r\n<\/span><span style=\"color: #808080;\">search_query <\/span><span style=\"color: #a71d5d;\">= <\/span><span style=\"color: #0086b3;\">SearchQuery<\/span><span style=\"color: #63a35c;\">(<\/span>query<span style=\"color: #63a35c;\">)\r\n#\u8fd1\u4f3c\u67e5\u8be2\u548c\u6743\u503crank\r\n<\/span><span style=\"color: #969896;\"># results = Post.published.annotate(\r\n<\/span><span style=\"color: #969896;\">            #     search=search_vector, rank=SearchRank(search_vector, search_query)\r\n<\/span><span style=\"color: #969896;\">            # ).filter(rank__gte=0.3).order_by('-rank')\r\n#\u00a0trigram\u76f8\u62df\u67e5\u8be2\r\n<\/span>results <span style=\"color: #a71d5d;\">= <\/span>Post<span style=\"color: #63a35c;\">.<\/span>published<span style=\"color: #63a35c;\">.<\/span><span style=\"color: #0086b3;\">annotate<\/span><span style=\"color: #63a35c;\">(\r\n<\/span><span style=\"color: #660099;\">similarity<\/span><span style=\"color: #a71d5d;\">=<\/span><span style=\"color: #0086b3;\">TrigramSimilarity<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"font-weight: bold; color: #008080;\">'title'<\/span><span style=\"color: #63a35c;\">, <\/span>query<span style=\"color: #63a35c;\">),\r\n<\/span><span style=\"color: #63a35c;\">            ).<\/span><span style=\"color: #0086b3;\">filter<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"color: #660099;\">similarity__gt<\/span><span style=\"color: #a71d5d;\">=<\/span><span style=\"color: #0086b3;\">0.1<\/span><span style=\"color: #63a35c;\">).<\/span><span style=\"color: #0086b3;\">order_by<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"font-weight: bold; color: #008080;\">'-similarity'<\/span><span style=\"color: #63a35c;\">)\r\n<\/span><span style=\"color: #a71d5d;\">return <\/span><span style=\"color: #0086b3;\">render<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"color: #0086b3;\">request<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'blog\/post\/search.html'<\/span><span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"color: #63a35c;\">                  {<\/span><span style=\"font-weight: bold; color: #008080;\">'form'<\/span><span style=\"color: #a71d5d;\">:<\/span>form<span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'query'<\/span><span style=\"color: #a71d5d;\">: <\/span>query<span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"font-weight: bold; color: #008080;\">'results'<\/span><span style=\"color: #a71d5d;\">: <\/span>results<span style=\"color: #63a35c;\">,\r\n<\/span><span style=\"color: #63a35c;\">                  }\r\n<\/span><span style=\"color: #63a35c;\">                  )<\/span><\/pre>\n<p>url\u4e2dpath<\/p>\n<pre><span style=\"color: #0086b3;\">path<\/span><span style=\"color: #63a35c;\">(<\/span><span style=\"font-weight: bold; color: #008080;\">'search\/'<\/span><span style=\"color: #63a35c;\">, <\/span>views<span style=\"color: #63a35c;\">.<\/span>post_search<span style=\"color: #63a35c;\">, <\/span><span style=\"color: #660099;\">name<\/span><span style=\"color: #a71d5d;\">=<\/span><span style=\"font-weight: bold; color: #008080;\">'post_search'<\/span><span style=\"color: #63a35c;\">),<\/span><\/pre>\n<p>\u6a21\u677f\u5c42 search.html<\/p>\n<pre>{% extends 'blog\/base.html' %}\r\n{% load blog_tags %}\r\n\r\n{% block title %} Search {% endblock %}\r\n\r\n{% block content %}\r\n    {% if query %}\r\n        &lt;<span style=\"color: #63a35c;\">h1<\/span>&gt;Posts containing {{ query }}&lt;\/<span style=\"color: #63a35c;\">h1<\/span>&gt;\r\n        &lt;<span style=\"color: #63a35c;\">h3<\/span>&gt;\r\n            {% with results.count as total_results %}\r\n                Found {{ total_results }} result {{ total_results|pluralize }}\r\n            {% endwith %}\r\n        &lt;\/<span style=\"color: #63a35c;\">h3<\/span>&gt;\r\n        {% for post in results %}\r\n            &lt;<span style=\"color: #63a35c;\">h4<\/span>&gt;&lt;<span style=\"color: #63a35c;\">a <\/span><span style=\"color: #795da3;\">href<\/span><span style=\"font-weight: bold; color: #183691;\">=\"{{ post.get_absolute_url }}\"<\/span>&gt;{{ post.title }}&lt;\/<span style=\"color: #63a35c;\">a<\/span>&gt; &lt;\/<span style=\"color: #63a35c;\">h4<\/span>&gt;\r\n            {{ post.body|markdown|truncatewords_html:5 }}\r\n        {% empty %}\r\n            &lt;<span style=\"color: #63a35c;\">p<\/span>&gt;There are no results for your query.&lt;\/<span style=\"color: #63a35c;\">p<\/span>&gt;\r\n        {% endfor %}\r\n        &lt;<span style=\"color: #63a35c;\">p<\/span>&gt;&lt;<span style=\"color: #63a35c;\">a <\/span><span style=\"color: #795da3;\">href<\/span><span style=\"font-weight: bold; color: #183691;\">=\"{% url 'blog:post_search' %}\"<\/span>&gt;Search again&lt;\/<span style=\"color: #63a35c;\">a<\/span>&gt; &lt;\/<span style=\"color: #63a35c;\">p<\/span>&gt;\r\n    {% else %}\r\n    &lt;<span style=\"color: #63a35c;\">h1<\/span>&gt;Search for posts&lt;\/<span style=\"color: #63a35c;\">h1<\/span>&gt;\r\n    &lt;<span style=\"color: #63a35c;\">form <\/span><span style=\"color: #795da3;\">method<\/span><span style=\"font-weight: bold; color: #183691;\">=\"get\"<\/span>&gt;\r\n        {{ form.as_p }}\r\n        &lt;<span style=\"color: #63a35c;\">input <\/span><span style=\"color: #795da3;\">type<\/span><span style=\"font-weight: bold; color: #183691;\">=\"submit\" <\/span><span style=\"color: #795da3;\">value<\/span><span style=\"font-weight: bold; color: #183691;\">=\"Search\"<\/span>&gt;\r\n    &lt;\/<span style=\"color: #63a35c;\">form<\/span>&gt;\r\n    {% endif %}\r\n{% endblock %}<\/pre>\n<p>\u5176\u5b83\u6269\u5c55\uff1a<\/p>\n<p>Haystack https: \/\/django-haystack.readthedocs.io\/en\/master\/<\/p>\n<p>\u68a6\u7fd4\u513f\uff0c\u6839\u636e2020\u5e74\u7b2c3\u7248\u82f1\u6587\u4e66\u5b8c\u6210\u5e76\u8bb0\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5\u89c1\u8fd9\u91cc\uff1a Windows\u4e0bPostgreSQL\u5efa\u65b0\u7528\u6237\u548c\u5e93\u3002 setting.py\u4e2d\u6570\u636e\u5e93\u8bbe DATABASES = { &#8216;default&#8217;: { #&#8217;ENGINE&#8217;: &#8216;django.db.backends.sqlite3&#8217;, #&#8217;N &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-django"],"_links":{"self":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288"}],"version-history":[{"count":2,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":291,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions\/291"}],"wp:attachment":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}