{"id":61195,"date":"2024-10-13T09:13:16","date_gmt":"2024-10-13T01:13:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61195\/"},"modified":"2024-10-13T09:13:16","modified_gmt":"2024-10-13T01:13:16","slug":"django%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%90%9c%e7%b4%a2%e5%8f%8a%e7%b5%90%e6%9e%9c%e5%b1%95%e7%a4%ba%e6%95%99%e7%a8%8b-django%e6%90%9c%e7%b4%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%a6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61195\/","title":{"rendered":"Django\u5be6\u73fe\u6578\u64da\u5eab\u641c\u7d22\u53ca\u7d50\u679c\u5c55\u793a\u6559\u7a0b (django\u641c\u7d22\u6578\u64da\u5eab\u4e26\u986f\u793a)"},"content":{"rendered":"<h1 id=\"django%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%90%9c%e7%b4%a2%e5%8f%8a%e7%b5%90%e6%9e%9c%e5%b1%95%e7%a4%ba%e6%95%99%e7%a8%8b-LvgtadHQPN\">Django\u5be6\u73fe\u6578\u64da\u5eab\u641c\u7d22\u53ca\u7d50\u679c\u5c55\u793a\u6559\u7a0b<\/h1>\n<p>Django\u662f\u4e00\u500b\u5f37\u5927\u7684Web\u6846\u67b6\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u69cb\u5efa\u9ad8\u6548\u7684Web\u61c9\u7528\u3002\u5728\u8a31\u591a\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u641c\u7d22\u529f\u80fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Django\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u641c\u7d22\u53ca\u7d50\u679c\u5c55\u793a\u7684\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-LvgtadHQPN\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86Django\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88dd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install django<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684Django\u9805\u76ee\uff1a<\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<p>\u7136\u5f8c\u9032\u5165\u9805\u76ee\u76ee\u9304\u4e26\u5275\u5efa\u4e00\u500b\u65b0\u7684\u61c9\u7528\uff1a<\/p>\n<pre><code>cd myproject\ndjango-admin startapp myapp<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%ae%e6%95%b8%e6%93%9a%e6%a8%a1%e5%9e%8b-LvgtadHQPN\">\u8a2d\u7f6e\u6578\u64da\u6a21\u578b<\/h2>\n<p>\u5728Django\u4e2d\uff0c\u6578\u64da\u6a21\u578b\u662f\u7528\u4f86\u5b9a\u7fa9\u6578\u64da\u7d50\u69cb\u7684\u3002\u5047\u8a2d\u6211\u5011\u8981\u641c\u7d22\u7684\u6578\u64da\u662f\u66f8\u7c4d\u4fe1\u606f\uff0c\u6211\u5011\u53ef\u4ee5\u5728myapp\u7684models.py\u4e2d\u5b9a\u7fa9\u4e00\u500bBook\u6a21\u578b\uff1a<\/p>\n<pre><code>from django.db import models\n\nclass Book(models.Model):\n    title = models.CharField(max_length=200)\n    author = models.CharField(max_length=100)\n    published_date = models.DateField()\n\n    def __str__(self):\n        return self.title<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5275\u5efa\u6578\u64da\u5eab\u8868\uff1a<\/p>\n<pre><code>python manage.py makemigrations\npython manage.py migrate<\/code><\/pre>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e6%90%9c%e7%b4%a2%e8%a1%a8%e5%96%ae-LvgtadHQPN\">\u5275\u5efa\u641c\u7d22\u8868\u55ae<\/h2>\n<p>\u70ba\u4e86\u5be6\u73fe\u641c\u7d22\u529f\u80fd\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u8868\u55ae\u3002\u5728myapp\u7684forms.py\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u5b9a\u7fa9\uff1a<\/p>\n<pre><code>from django import forms\n\nclass SearchForm(forms.Form):\n    query = forms.CharField(label='Search', max_length=100)<\/code><\/pre>\n<h2 id=\"%e7%b7%a8%e5%af%ab%e8%a6%96%e5%9c%96%e5%87%bd%e6%95%b8-LvgtadHQPN\">\u7de8\u5beb\u8996\u5716\u51fd\u6578<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u7de8\u5beb\u4e00\u500b\u8996\u5716\u51fd\u6578\u4f86\u8655\u7406\u641c\u7d22\u8acb\u6c42\u3002\u5728myapp\u7684views.py\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u5be6\u73fe\uff1a<\/p>\n<pre><code>from django.shortcuts import render\nfrom .models import Book\nfrom .forms import SearchForm\n\ndef search(request):\n    form = SearchForm()\n    results = []\n    \n    if 'query' in request.GET:\n        form = SearchForm(request.GET)\n        if form.is_valid():\n            query = form.cleaned_data['query']\n            results = Book.objects.filter(title__icontains=query)\n    \n    return render(request, 'myapp\/search.html', {'form': form, 'results': results})<\/code><\/pre>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e6%a8%a1%e6%9d%bf-LvgtadHQPN\">\u5275\u5efa\u6a21\u677f<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u6a21\u677f\u4f86\u986f\u793a\u641c\u7d22\u8868\u55ae\u548c\u7d50\u679c\u3002\u5728myapp\u7684templates\/myapp\/search.html\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u7de8\u5beb\uff1a<\/p>\n<pre><code>&lt;h1&gt;\u66f8\u7c4d\u641c\u7d22&lt;\/h1&gt;\n&lt;form method=\"get\"&gt;\n    {{ form.as_p }}\n    &lt;button type=\"submit\"&gt;\u641c\u7d22&lt;\/button&gt;\n&lt;\/form&gt;\n\n&lt;h2&gt;\u641c\u7d22\u7d50\u679c&lt;\/h2&gt;\n&lt;ul&gt;\n    {% for book in results %}\n        &lt;li&gt;{{ book.title }} - {{ book.author }} ({{ book.published_date }})&lt;\/li&gt;\n    {% empty %}\n        &lt;li&gt;\u6c92\u6709\u627e\u5230\u7d50\u679c&lt;\/li&gt;\n    {% endfor %}\n&lt;\/ul&gt;<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%aeurl-LvgtadHQPN\">\u914d\u7f6eURL<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5728myapp\u7684urls.py\u4e2d\u914d\u7f6eURL\u8def\u7531\uff1a<\/p>\n<pre><code>from django.urls import path\nfrom .views import search\n\nurlpatterns = [\n    path('search\/', search, name='search'),\n]<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LvgtadHQPN\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u6210\u529f\u5730\u5728Django\u4e2d\u5be6\u73fe\u4e86\u6578\u64da\u5eab\u641c\u7d22\u53ca\u7d50\u679c\u5c55\u793a\u7684\u529f\u80fd\u3002\u9019\u500b\u529f\u80fd\u53ef\u4ee5\u6839\u64da\u7528\u6236\u7684\u8f38\u5165\u5728\u6578\u64da\u5eab\u4e2d\u67e5\u627e\u76f8\u95dc\u7684\u66f8\u7c4d\u4fe1\u606f\uff0c\u4e26\u5c07\u7d50\u679c\u986f\u793a\u5728\u7db2\u9801\u4e0a\u3002Django\u7684\u5f37\u5927\u4e4b\u8655\u5728\u65bc\u5176\u7c21\u55ae\u6613\u7528\u7684API\u548c\u9748\u6d3b\u7684\u67b6\u69cb\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u51fa\u529f\u80fd\u8c50\u5bcc\u7684Web\u61c9\u7528\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u4f60\u7684Django\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52d9\u5668<\/a> \u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Django\u5be6\u73fe\u6578\u64da\u5eab\u641c\u7d22\u529f\u80fd\uff0c\u4e26\u6709\u6548\u5c55\u793a\u641c\u7d22\u7d50\u679c\u7684\u8a73\u7d30\u6559\u7a0b\uff0c\u63d0\u5347\u4f60\u7684Web\u958b\u767c\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61195","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=61195"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61195\/revisions"}],"predecessor-version":[{"id":61196,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61195\/revisions\/61196"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}