{"id":90,"date":"2020-11-19T09:22:03","date_gmt":"2020-11-19T03:22:03","guid":{"rendered":"http:\/\/www.dreamflier.net\/wordpress\/?p=90"},"modified":"2021-01-14T19:52:54","modified_gmt":"2021-01-14T13:52:54","slug":"%e3%80%8apython%e7%bc%96%e7%a8%8b%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e8%b7%b5%e3%80%8b18%ef%bc%8c19%e7%ab%a0-django-%e6%96%b0%e7%89%88%e6%9c%ac%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%94%b9","status":"publish","type":"post","link":"http:\/\/www.jichangqing.net\/wordpress\/?p=90","title":{"rendered":"\u300apython\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b\u7b2c\u4e00\u7248 18\uff0c19\u7ae0 django \u65b0\u7248\u672c\u4ee3\u7801\u66f4\u6539"},"content":{"rendered":"<p style=\"color: #4d4d4d;\">\u300aPython\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b18\uff0c19\u7ae0 django \u65b0\u7248\u672c\u4ee3\u7801\u66f4\u6539 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \uff082020.5\uff09<\/p>\n<p style=\"color: #4d4d4d;\">\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u70b9\u4e2a\u8d5e\u652f\u6301\u4e00\u4e0b\uff0c\u87f9\u87f9<\/p>\n<p style=\"color: #4d4d4d;\">18.2.4 \u5b9a\u4e49\u6a21\u578bEntry<\/p>\n<p style=\"color: #4d4d4d;\">model.py<\/p>\n<pre style=\"color: #000000;\"><code class=\"language-python hljs\" style=\"color: #383a42;\">topic=models.ForeignKey(Topic)<\/code><\/pre>\n<p style=\"color: #4d4d4d;\">\u6539\u4e3a<\/p>\n<pre style=\"color: #000000;\"><code class=\"language-python hljs\" style=\"color: #383a42;\">topic=models.ForeignKey(Topic,on_delete=models.CASCADE)<\/code><\/pre>\n<p style=\"color: #4d4d4d;\">\u5728\u540e\u976219.3.2 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u5916\u952e\u90fd\u9700\u8981\u6dfb\u52a0on_delete=models.CASCADE<\/p>\n<p style=\"color: #4d4d4d;\">models.py<\/p>\n<p style=\"color: #4d4d4d;\">owner=models.ForeignKey(User,on_delete=models.CASCADE)<\/p>\n<p style=\"color: #4d4d4d;\">18.3.1\u6620\u5c04URL<\/p>\n<ol class=\"hljs-ln\">\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.conf.urls <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> include,url<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.contrib <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> admin<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">urlpatterns=[<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">url(<span class=\"hljs-string\" style=\"color: #50a14f;\">r&#8217;^admin\/,include(admin.site.urls)),<\/span><\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\"> url(r&#8217;<\/span><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;,include(&#8216;<\/span>learning_logs.urls<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;,namespace=&#8217;<\/span>learning_logs<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;)),<\/span><\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\">]<\/span><\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p style=\"color: #4d4d4d;\">\u6539\u4e3a<\/p>\n<ol class=\"hljs-ln\">\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.urls <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> path\uff0cinclude<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">&#8230;&#8230;<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">urlpatterns=[<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">path(<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;admin\/&#8217;<\/span>,admin.site.urls),<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">path(<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8221;<\/span>,include((<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;learning_logs.urls&#8217;<\/span>,<strong><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;learning_logs&#8217;<\/span><\/strong>),<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">namespace=<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;learning_logs&#8217;<\/span>)),<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">]<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\"><span class=\"hljs-string\" style=\"color: #50a14f;\">\u68a6\u7fd4\u513f\u6ce8:&#8217;learning_logs&#8217;\u4e0d\u52a0\u4f1a404\u9519\u4f4d<\/span><\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u91cd\u590d\u7684\u5730\u65b9\u4e0d\u505a\u4fee\u6539\u4e86\uff0curl\u6539\u4e3apath \u628a&#8217;r^$&#8217; \u4e09\u4e2a\u7b26\u53f7\u53bb\u6389\uff0c\u540e\u9762\u90fd\u8fd9\u6837\u6539<\/p>\n<p style=\"color: #4d4d4d;\">\u540e\u9762\u7684 \u8f6c\u5230user.urls\u4e0e\u8f6c\u5230learning_logs.urls\u5f62\u5f0f\u4e00\u6837<\/p>\n<p style=\"color: #4d4d4d;\">19.1.1<\/p>\n<p style=\"color: #4d4d4d;\">views.py<\/p>\n<pre style=\"color: #000000;\"><code class=\"language-python hljs\" style=\"color: #383a42;\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.core.urlresolvers <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> reverse<\/code><\/pre>\n<p style=\"color: #4d4d4d;\">\u6539\u4e3a<\/p>\n<pre style=\"color: #000000;\"><code class=\"hljs javascript\" style=\"color: #383a42;\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.urls <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> reverse<\/code><\/pre>\n<p style=\"color: #4d4d4d;\">\u68a6\u7fd4\u513f \u6ce8:<\/p>\n<p style=\"color: #4d4d4d;\">from django.shortcuts import render<\/p>\n<p style=\"color: #4d4d4d;\">from django.http import HttpResponseRedirect<\/p>\n<p style=\"color: #4d4d4d;\">\u6539\u4e3a<\/p>\n<p style=\"color: #4d4d4d;\">from django.shortcuts import render, redirect<\/p>\n<p style=\"color: #4d4d4d;\">19.2.2<\/p>\n<p style=\"color: #4d4d4d;\">urls.py<\/p>\n<ol class=\"hljs-ln\">\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.contrib.auth.views <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> login<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\"><\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">&#8230;..<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\"><\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">url(<span class=\"hljs-string\" style=\"color: #50a14f;\">r&#8217;^login\/$&#8217;<\/span>,login,{template_name<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;:&#8217;<\/span>user\/login.html},name=<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;login&#8217;<\/span>)<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p style=\"color: #4d4d4d;\">\u6539\u4e3a<\/p>\n<ol class=\"hljs-ln\">\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-keyword\" style=\"color: #a626a4;\">from<\/span> django.contrib.auth.views <span class=\"hljs-keyword\" style=\"color: #a626a4;\">import<\/span> LoginView<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\"><\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">\u3002\u3002\u3002\u3002\u3002<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\"><\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">path(<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;login\/&#8217;<\/span>,LoginView.as_view(template_name=<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;users\/login.html&#8217;<\/span>),<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">name=<span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;login&#8217;<\/span>),<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p style=\"color: #4d4d4d;\">\u5e76\u4e14\u8981\u5728\u9879\u76eesetting.py\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c<\/p>\n<p style=\"color: #4d4d4d;\">&#8216;DIRS&#8217;: [os.path.join(BASE_DIR, &#8216;users\/Templates&#8217;)], \u00a0 \u00a0\u00a0 \u76ee\u7684\u662f\u8ba9django\u627e\u5230 template_name \u4e2d\u7684\u5730\u5740 \u5728\u90a3\u4e2a\u7a0b\u5e8f\u91cc<\/p>\n<ol class=\"hljs-ln\">\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">DATABASES = {<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;default&#8217;<\/span>: {<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;ENGINE&#8217;<\/span>: <span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;django.db.backends.sqlite3&#8217;<\/span>,<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;NAME&#8217;<\/span>: <span class=\"hljs-built_in\" style=\"color: #c18401;\">os<\/span>.<span class=\"hljs-built_in\" style=\"color: #c18401;\">path<\/span>.join(BASE_DIR, <span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;db.sqlite3&#8217;<\/span>),<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\"><span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;DIRS&#8217;<\/span>: [<span class=\"hljs-built_in\" style=\"color: #c18401;\">os<\/span>.<span class=\"hljs-built_in\" style=\"color: #c18401;\">path<\/span>.join(BASE_DIR, <span class=\"hljs-string\" style=\"color: #50a14f;\">&#8216;users\/Templates&#8217;<\/span>)],<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">}<\/div>\n<\/div>\n<\/li>\n<li>\n<div class=\"hljs-ln-numbers\"><\/div>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-line\">}<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p style=\"color: #4d4d4d;\">\u5176\u4ed6\u5730\u65b9\u51fa\u9519\uff0c\u5e94\u8be5\u5c31\u662f\u4f60\u6572\u9519\u4ee3\u7801\u4e86^ ^\u00a0 \u5b8c\u6210\u5982\u4e0b<\/p>\n<p style=\"color: #4d4d4d;\"><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200508180159879.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E5NDExNDIwNzQ=,size_16,color_FFFFFF,t_70\" alt=\"\" \/><\/p>\n<p style=\"color: #4d4d4d;\">from:https:\/\/blog.csdn.net\/a941142074\/article\/details\/106001577?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aPython\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b18\uff0c19\u7ae0 django \u65b0\u7248\u672c\u4ee3\u7801\u66f4\u6539 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \uff082020.5\uff09 \u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u70b9\u4e2a\u8d5e\u652f\u6301\u4e00\u4e0b\uff0c\u87f9\u87f9 18.2.4 \u5b9a\u4e49\u6a21\u578bEntry model.py topic=models.Fore &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-90","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\/90","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=90"}],"version-history":[{"count":5,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":95,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/95"}],"wp:attachment":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}