{"id":1095402,"date":"2025-01-08T14:50:45","date_gmt":"2025-01-08T06:50:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095402.html"},"modified":"2025-01-08T14:50:47","modified_gmt":"2025-01-08T06:50:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python3%e5%bc%80%e5%8f%91%e7%bd%91%e7%ab%99-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095402.html","title":{"rendered":"\u5982\u4f55\u7528python3\u5f00\u53d1\u7f51\u7ad9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211053\/d01f944e-f2d7-489a-84dc-c96ba1d4408e.webp\" alt=\"\u5982\u4f55\u7528python3\u5f00\u53d1\u7f51\u7ad9\" \/><\/p>\n<p><p> <strong>\u7528Python3\u5f00\u53d1\u7f51\u7ad9\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528Django\u6846\u67b6\u3001\u4f7f\u7528Flask\u6846\u67b6\u3001\u4f7f\u7528Pyramid\u6846\u67b6\u3001\u4f7f\u7528Web2py\u6846\u67b6\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Django<\/strong>\u6846\u67b6\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u4e30\u5bcc\uff0c\u6587\u6863\u9f50\u5168\uff0c\u9002\u5408\u5f00\u53d1\u4e2d\u5927\u578b\u7f51\u7ad9\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Django\u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u8bbe\u7f6eDjango\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4efb\u4f55\u5f00\u53d1\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u8bbe\u7f6eDjango\u73af\u5883\u3002Django\u662f\u4e00\u4e2a\u9ad8\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u9f13\u52b1\u5feb\u901f\u5f00\u53d1\u548c\u5e72\u51c0\u3001\u5b9e\u7528\u7684\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python\u548cDjango<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Python 3\u3002\u4f60\u53ef\u4ee5\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.python.org\/downloads\/\">Python\u5b98\u65b9\u9875\u9762<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5408\u9002\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\u5b89\u88c5Django\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip3 install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaDjango\u9879\u76ee<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\uff1a\n<pre><code class=\"language-bash\">django-admin startproject mysite<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mysite<\/code>\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542bDjango\u9879\u76ee\u7684\u57fa\u672c\u7ed3\u6784\u3002<\/li>\n<\/p>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\u5e76\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff0c\u4ee5\u786e\u4fdd\u4e00\u5207\u5b89\u88c5\u6b63\u786e\uff1a\n<pre><code class=\"language-bash\">cd mysite<\/p>\n<p>python3 manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5c06\u770b\u5230Django\u5f00\u53d1\u670d\u52a1\u5668\u6b63\u5728\u8fd0\u884c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000\/<\/code>\u3002<\/li>\n<\/p>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u548c\u914d\u7f6eDjango\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728Django\u9879\u76ee\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u662f\u9879\u76ee\u7684\u4e00\u4e2a\u5b50\u6a21\u5757\u3002\u6bcf\u4e2a\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5904\u7406\u7279\u5b9a\u7684\u529f\u80fd\u6216\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaDjango\u5e94\u7528<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u5e94\u7528\uff1a\n<pre><code class=\"language-bash\">python3 manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myapp<\/code>\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u7ed3\u6784\u3002<\/li>\n<\/p>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u5e94\u7528<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u9879\u76ee\u76ee\u5f55\u4e0b\u7684<code>settings.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5c06\u65b0\u521b\u5efa\u7684\u5e94\u7528\u6dfb\u52a0\u5230<code>INSTALLED_APPS<\/code>\u5217\u8868\u4e2d\uff1a\n<pre><code class=\"language-python\">INSTALLED_APPS = [<\/p>\n<p>    ...<\/p>\n<p>    &#39;myapp&#39;,<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b9a\u4e49\u6a21\u578b\uff08Models\uff09<\/h3>\n<\/p>\n<p><p>\u6a21\u578b\u662fDjango\u4e2d\u5904\u7406\u6570\u636e\u5e93\u7684\u90e8\u5206\u3002\u5b83\u4eec\u5b9a\u4e49\u4e86\u6570\u636e\u5e93\u7684\u7ed3\u6784\u548c\u6570\u636e\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u6a21\u578b<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00<code>myapp\/models.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u535a\u5ba2\u6587\u7ae0\u6a21\u578b\uff1a\n<pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Post(models.Model):<\/p>\n<p>    title = models.CharField(max_length=200)<\/p>\n<p>    content = models.TextField()<\/p>\n<p>    created_at = models.DateTimeField(auto_now_add=True)<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return self.title<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fc1\u79fb\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u5b9a\u4e49\u597d\u6a21\u578b\u540e\uff0c\u9700\u8981\u5c06\u8fd9\u4e9b\u66f4\u6539\u5e94\u7528\u5230\u6570\u636e\u5e93\u4e2d\u3002\u9996\u5148\u751f\u6210\u8fc1\u79fb\u6587\u4ef6\uff1a\n<pre><code class=\"language-bash\">python3 manage.py makemigrations<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u7136\u540e\u5e94\u7528\u8fd9\u4e9b\u8fc1\u79fb\uff1a<\/p>\n<pre><code class=\"language-bash\">python3 manage.py migrate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u521b\u5efa\u89c6\u56fe\uff08Views\uff09<\/h3>\n<\/p>\n<p><p>\u89c6\u56fe\u662fDjango\u4e2d\u5904\u7406\u8bf7\u6c42\u548c\u8fd4\u56de\u54cd\u5e94\u7684\u90e8\u5206\u3002\u5b83\u4eec\u51b3\u5b9a\u4e86\u7528\u6237\u5728\u8bbf\u95ee\u7279\u5b9aURL\u65f6\u770b\u5230\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u89c6\u56fe<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00<code>myapp\/views.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u89c6\u56fe\u3002\u4f8b\u5982\uff0c\u663e\u793a\u6240\u6709\u535a\u5ba2\u6587\u7ae0\uff1a\n<pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>from .models import Post<\/p>\n<p>def index(request):<\/p>\n<p>    posts = Post.objects.all()<\/p>\n<p>    return render(request, &#39;index.html&#39;, {&#39;posts&#39;: posts})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eURL<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p>\u6253\u5f00<code>mysite\/urls.py<\/code>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u5e94\u7528\u7684URL\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import path, include<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, include(&#39;myapp.urls&#39;)),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\u5728<code>myapp<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>urls.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5b9a\u4e49\u5e94\u7528\u7684URL\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.urls import path<\/p>\n<p>from . import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;&#39;, views.index, name=&#39;index&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u521b\u5efa\u6a21\u677f\uff08Templates\uff09<\/h3>\n<\/p>\n<p><p>\u6a21\u677f\u662fDjango\u4e2d\u5904\u7406HTML\u7684\u90e8\u5206\u3002\u5b83\u4eec\u5b9a\u4e49\u4e86\u7f51\u9875\u7684\u5916\u89c2\u548c\u5e03\u5c40\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u6a21\u677f\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728<code>myapp<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>templates<\/code>\u7684\u76ee\u5f55\uff0c\u7136\u540e\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2a<code>index.html<\/code>\u6587\u4ef6\uff1a\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;My Blog&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;My Blog&lt;\/h1&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>    {% for post in posts %}<\/p>\n<p>        &lt;li&gt;{{ post.title }} - {{ post.created_at }}&lt;\/li&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6e32\u67d3\u6a21\u677f<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u89c6\u56fe\u51fd\u6570\u4e2d\u6b63\u786e\u5730\u6e32\u67d3\u6a21\u677f\u6587\u4ef6\uff0c\u5e76\u4f20\u9012\u4e0a\u4e0b\u6587\u53d8\u91cf\uff1a\n<pre><code class=\"language-python\">return render(request, &#39;index.html&#39;, {&#39;posts&#39;: posts})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u9759\u6001\u6587\u4ef6\uff08Static Files\uff09<\/h3>\n<\/p>\n<p><p>\u9759\u6001\u6587\u4ef6\u5305\u62ecCSS\u3001JavaScript\u3001\u56fe\u7247\u7b49\u3002Django\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u7ba1\u7406\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6e\u9759\u6001\u6587\u4ef6\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00<code>settings.py<\/code>\u6587\u4ef6\uff0c\u5e76\u914d\u7f6e\u9759\u6001\u6587\u4ef6\u76ee\u5f55\uff1a\n<pre><code class=\"language-python\">STATIC_URL = &#39;\/static\/&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u9759\u6001\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728<code>myapp<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>static<\/code>\u7684\u76ee\u5f55\uff0c\u7136\u540e\u5728\u5176\u4e2d\u521b\u5efaCSS\u548cJavaScript\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>style.css<\/code>\u6587\u4ef6\uff1a\n<pre><code class=\"language-css\">body {<\/p>\n<p>    font-family: Arial, sans-serif;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u52a0\u8f7d\u9759\u6001\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u52a0\u8f7d\u9759\u6001\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u52a0\u8f7dCSS\u6587\u4ef6\uff1a\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;My Blog&lt;\/title&gt;<\/p>\n<p>    &lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;{% static &#39;style.css&#39; %}&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;My Blog&lt;\/h1&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>    {% for post in posts %}<\/p>\n<p>        &lt;li&gt;{{ post.title }} - {{ post.created_at }}&lt;\/li&gt;<\/p>\n<p>    {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f7f\u7528Django\u7ba1\u7406\u540e\u53f0<\/h3>\n<\/p>\n<p><p>Django\u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u7ba1\u7406\u540e\u53f0\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u6a21\u578b\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u8d85\u7ea7\u7528\u6237<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u8d85\u7ea7\u7528\u6237\uff1a\n<pre><code class=\"language-bash\">python3 manage.py createsuperuser<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6ce8\u518c\u6a21\u578b<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00<code>myapp\/admin.py<\/code>\u6587\u4ef6\uff0c\u5e76\u6ce8\u518c\u6a21\u578b\uff1a\n<pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from .models import Post<\/p>\n<p>admin.site.register(Post)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbf\u95ee\u7ba1\u7406\u540e\u53f0<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff0c\u5e76\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000\/admin\/<\/code>\uff0c\u4f7f\u7528\u521b\u5efa\u7684\u8d85\u7ea7\u7528\u6237\u8d26\u6237\u767b\u5f55\uff0c\u5373\u53ef\u7ba1\u7406\u6a21\u578b\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>Django\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u6d4b\u8bd5\u548c\u8c03\u8bd5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199\u6d4b\u8bd5<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728<code>myapp\/tests.py<\/code>\u6587\u4ef6\u4e2d\u7f16\u5199\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u6d4b\u8bd5\u89c6\u56fe\u51fd\u6570\uff1a\n<pre><code class=\"language-python\">from django.test import TestCase<\/p>\n<p>from django.urls import reverse<\/p>\n<p>class PostViewTests(TestCase):<\/p>\n<p>    def test_index_view(self):<\/p>\n<p>        response = self.client.get(reverse(&#39;index&#39;))<\/p>\n<p>        self.assertEqual(response.status_code, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\uff1a\n<pre><code class=\"language-bash\">python3 manage.py test<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528Python\u8c03\u8bd5\u5668\uff08\u5982<code>pdb<\/code>\uff09\u8c03\u8bd5\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u6dfb\u52a0\u65ad\u70b9\uff1a\n<pre><code class=\"language-python\">import pdb; pdb.set_trace()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u90e8\u7f72Django\u7f51\u7ad9<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5c06Django\u7f51\u7ad9\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u90e8\u7f72\u5e73\u53f0<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u5e73\u53f0\u6765\u90e8\u7f72Django\u7f51\u7ad9\uff0c\u4f8b\u5982Heroku\u3001AWS\u3001DigitalOcean\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u751f\u4ea7\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728<code>settings.py<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6e\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\uff0c\u4f8b\u5982\uff1a\n<pre><code class=\"language-python\">DEBUG = False<\/p>\n<p>ALLOWED_HOSTS = [&#39;yourdom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.com&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528WSGI\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528WSGI\u670d\u52a1\u5668\uff08\u5982Gunicorn\uff09\u6765\u8fd0\u884cDjango\u5e94\u7528\uff1a\n<pre><code class=\"language-bash\">pip3 install gunicorn<\/p>\n<p>gunicorn mysite.wsgi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u53cd\u5411\u4ee3\u7406<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff08\u5982Nginx\uff09\u6765\u5904\u7406\u8bf7\u6c42\uff0c\u5e76\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9WSGI\u670d\u52a1\u5668\u3002\u4f8b\u5982\uff0cNginx\u914d\u7f6e\uff1a\n<pre><code class=\"language-nginx\">server {<\/p>\n<p>    listen 80;<\/p>\n<p>    server_name yourdomain.com;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass http:\/\/127.0.0.1:8000;<\/p>\n<p>        proxy_set_header Host $host;<\/p>\n<p>        proxy_set_header X-Real-IP $remote_addr;<\/p>\n<p>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n<p>        proxy_set_header X-Forwarded-Proto $scheme;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7528Python3\u5f00\u53d1\u7f51\u7ad9\u7684\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5b89\u88c5\u548c\u8bbe\u7f6eDjango\u73af\u5883\u3001\u521b\u5efa\u548c\u914d\u7f6eDjango\u5e94\u7528\u3001\u5b9a\u4e49\u6a21\u578b\u3001\u521b\u5efa\u89c6\u56fe\u3001\u521b\u5efa\u6a21\u677f\u3001\u9759\u6001\u6587\u4ef6\u7ba1\u7406\u3001\u4f7f\u7528Django\u7ba1\u7406\u540e\u53f0\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3001\u90e8\u7f72Django\u7f51\u7ad9<\/strong>\u3002Django\u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4f7f\u5f97\u5f00\u53d1\u548c\u90e8\u7f72\u8fc7\u7a0b\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72\u529f\u80fd\u4e30\u5bcc\u3001\u6027\u80fd\u4f18\u8d8a\u7684Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u6846\u67b6\u6765\u5f00\u53d1\u7f51\u7ad9\uff1f<\/strong><br \/>\u5728\u9009\u62e9Python\u6846\u67b6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u51e0\u4e2a\u6d41\u884c\u9009\u9879\uff0c\u5982Django\u548cFlask\u3002Django\u9002\u5408\u9700\u8981\u5feb\u901f\u5f00\u53d1\u548c\u9ad8\u5b89\u5168\u6027\u7684\u9879\u76ee\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u529f\u80fd\u3002Flask\u5219\u66f4\u7075\u6d3b\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u6216\u9700\u8981\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u7684\u5e94\u7528\u3002\u6839\u636e\u9879\u76ee\u7684\u89c4\u6a21\u3001\u590d\u6742\u6027\u548c\u56e2\u961f\u7684\u719f\u6089\u7a0b\u5ea6\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u6846\u67b6\u3002<\/p>\n<p><strong>\u5728Python\u7f51\u7ad9\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u591a\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u5f0f\uff0c\u5e38\u7528\u7684\u6709ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\uff0c\u5982Django\u7684ORM\u548cSQLAlchemy\u3002\u4f7f\u7528ORM\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u901a\u8fc7Python\u5bf9\u8c61\u800c\u975eSQL\u8bed\u53e5\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u7b80\u5316\u4e86\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\uff08\u5982PostgreSQL\u3001MySQL\u6216SQLite\uff09\u4e5f\u81f3\u5173\u91cd\u8981\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u3002<\/p>\n<p><strong>\u7f51\u7ad9\u5f00\u53d1\u540e\uff0c\u5982\u4f55\u8fdb\u884c\u90e8\u7f72\u548c\u7ef4\u62a4\uff1f<\/strong><br \/>\u7f51\u7ad9\u5f00\u53d1\u5b8c\u6210\u540e\uff0c\u90e8\u7f72\u8fc7\u7a0b\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u670d\u52a1\u5668\uff08\u5982AWS\u3001Heroku\u6216DigitalOcean\uff09\u3001\u8bbe\u7f6e\u57df\u540d\u548cSSL\u8bc1\u4e66\u7b49\u3002\u7ef4\u62a4\u65b9\u9762\uff0c\u5b9a\u671f\u66f4\u65b0\u4ee3\u7801\u4ee5\u4fee\u590d\u6f0f\u6d1e\u548c\u6dfb\u52a0\u65b0\u529f\u80fd\u662f\u5fc5\u8981\u7684\u3002\u540c\u65f6\uff0c\u76d1\u63a7\u7f51\u7ad9\u6027\u80fd\u548c\u7528\u6237\u53cd\u9988\u4e5f\u80fd\u5e2e\u52a9\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff0c\u786e\u4fdd\u7f51\u7ad9\u957f\u671f\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python3\u5f00\u53d1\u7f51\u7ad9\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528Django\u6846\u67b6\u3001\u4f7f\u7528Flask\u6846\u67b6\u3001\u4f7f\u7528Pyramid\u6846\u67b6\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1095411,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095402"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1095402"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095402\/revisions"}],"predecessor-version":[{"id":1095413,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095402\/revisions\/1095413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095411"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}