{"id":84,"date":"2023-04-18T16:24:04","date_gmt":"2023-04-18T08:24:04","guid":{"rendered":"https:\/\/blog.maoyulong.club\/?p=84"},"modified":"2023-04-18T16:24:57","modified_gmt":"2023-04-18T08:24:57","slug":"django-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/blog.maoyulong.club\/?p=84","title":{"rendered":"Django \u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<p>\u4e4b\u524d\u505a\u8fc7\u4e00\u4e9bDjango \u7684\u6a21\u5757\u5f00\u53d1\uff0c\u6bd5\u8bbe\u4f1a\u4ece\u5934\u53bb\u505a\u4e00\u4e2a\u540e\u7aef\uff0c\u6240\u4ee5\u8bb0\u5f55\u4e00\u4e0b\u6700\u5f00\u59cb\u6784\u5efa\u7684\u5751<\/p>\n<ol start=\"\">\n<li>\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u5f53\u4f60\u6b63\u5e38\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff08\u5373<code>startproject<\/code>\uff09\uff0c\u90a3\u4e48wsgi.py \u4e2d\u5c06\u4f1a\u628a\u914d\u7f6e\u6587\u4ef6\u6307\u5411settings\uff0c\u8bb2\u5176\u4e2d\u7684<code>DATABASES<\/code> \u66ff\u6362\u4e3a\u4f60\u7684\u914d\u7f6e\uff0c\u5982\u679c\u662fmysql\u6216\u8005MariaDB\uff0c\u914d\u7f6e\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837<\/p>\n<pre><code class=\"language-python\" lang=\"python\">DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.mysql',\n        'NAME': 'database',\n        'USER': 'root',\n        'PASSWORD': 'xxxxxx',\n        'HOST': 'xxx.xxx.xx.x',\n        'PORT': '3306',\n    },\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u662fmysql\u6216\u8005MariaDB\uff0c\u8fd9\u4e4b\u540e\u9700\u8981\u7ee7\u7eed\u5b89\u88c5\u9a71\u52a8<a href=\"https:\/\/pypi.org\/project\/mysqlclient\/\" target=\"_blank\"  rel=\"nofollow\" >mysqlclient<\/a> \uff0c\u5982\u679c\u662fwindows\u7528\u6237\uff0c\u51fa\u73b0\u4e86 <code>\u65e0\u6cd5\u6253\u5f00\u5305\u62ec\u6587\u4ef6: \u201cmysql.h\u201d<\/code> \u7c7b\u4f3c\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4e0b\u8f7d<a class=\"url\" href=\"https:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#mysqlclient\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#mysqlclient<\/a> \u8fdb\u884c\u624b\u52a8\u5b89\u88c5\uff08pip install \u6587\u4ef6\u540d \u5373\u53ef\uff09<\/p>\n<p>\u5982\u679c\u662f\u5176\u4ed6\u7684\u6570\u636e\u5e93\uff0c\u53c2\u8003<a class=\"url\" href=\"https:\/\/docs.djangoproject.com\/zh-hans\/4.1\/ref\/databases\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/docs.djangoproject.com\/zh-hans\/4.1\/ref\/databases<\/a><\/p>\n<ol start=\"2\">\n<li>\u7b80\u6613\u7684\u767b\u5f55<\/li>\n<\/ol>\n<p>Django \u81ea\u5e26\u4e00\u4e2a\u7528\u6237\u9a8c\u8bc1\u7cfb\u7edf\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5df2\u7ecf\u5b89\u88c5\uff0c\u5e76\u5728\u914d\u7f6e\u5b8c\u6570\u636e\u5e93\u540e\u7b2c\u4e00\u6b21migrate\u540e\u751f\u6210\u76f8\u5173\u7684\u6570\u636e\u5e93\u8868\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528\uff1f\u4e3b\u8981\u53c2\u8003<\/p>\n<p><a href=\"https:\/\/docs.djangoproject.com\/zh-hans\/4.1\/topics\/auth\/default\/\" target=\"_blank\"  rel=\"nofollow\" >\u4f7f\u7528 Django \u7684\u9a8c\u8bc1\u7cfb\u7edf | Django \u6587\u6863 | Django (djangoproject.com)<\/a><\/p>\n<ol start=\"3\">\n<li>\u524d\u540e\u7aef\u5206\u79bb\u7684\u767b\u5f55<\/li>\n<\/ol>\n<p>\u5f88\u663e\u7136\u4e0a\u8ff0\u7684\u65b9\u6cd5\u5f88\u96be\u5b9e\u73b0\u524d\u540e\u7aef\u5206\u79bb\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u522b\u7684\u65b9\u6848<\/p>\n<p>Django\u5728\u505a\u524d\u540e\u7aef\u5206\u79bb\u5f00\u53d1\u7684\u65f6\u5019\u6709\u4e00\u4e2a\u7279\u522b\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5305<code>Django REST framework<\/code>\uff0c\u7528\u4e8e\u6784\u5efaWeb API<\/p>\n<p>\u518d\u52a0\u4e0adjangorestframework-jwt\u63d2\u4ef6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86<\/p>\n<p>\u53c2\u8003<\/p>\n<p><a href=\"https:\/\/www.cnblogs.com\/angelyan\/p\/10902551.html\" target=\"_blank\"  rel=\"nofollow\" >django-rest-framework-jwt\u7684\u4f7f\u7528 - Maple_feng - \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/p>\n<p>\u8fd9\u4e4b\u540e\uff0c\u901a\u8fc7View\u7684\u5b50\u7c7bAPIView\u8c03\u7528\uff0c\u5e76\u8bbe\u7f6eauthentication_classes\u5373\u53ef\uff0c\u5168\u5c40\u89c1\u4e0b\u6587<\/p>\n<ol start=\"4\">\n<li>\u5c42\u7ea7\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u53c8\u4e0a\u6211\u4eec\u5f97\u5230\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7cfb\u7edf\uff0c\u53ef\u80fd\u7531\u4e8e\u7528\u6237\u8868\u7b49\u539f\u56e0\uff0c\u5df2\u7ecf\u5efa\u7acb\u4e86\u4e00\u4e2a\u76f8\u5173\u7684app\uff0cdjango\u7684\u5173\u7cfb\u53ef\u4ee5\u7531\u7b80\u5355\u7684app\u6982\u62ec<\/p>\n<p>\u901a\u8fc7\u547d\u4ee4\u5efa\u7acbapp\uff08<code>django startapp<\/code>\uff09\uff0c\u5e76\u5728\u4e3b\u76ee\u5f55\u5c06url.py\u5f15\u5bfc\u8fc7\u6765\u5373\u53ef<\/p>\n<p>\u6ce8\u610f\uff0c\u6b64\u65f6\u8be5app\u7684\u6a21\u578b\u8868\u5e76\u6ca1\u6709\u540c\u6b65\uff0c\u9700\u8981makemigrations\u548cmigrate<\/p>\n<p>migrate\u9ed8\u8ba4\u662f\u6240\u6709\u7684app\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u901a\u8fc7\u540e\u9762\u6307\u5b9aapp\u6765\u5e94\u7528\u4fee\u6539<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">python manage.py makemigrations [appname]\npython manage.py migrate [appname]\n<\/code><\/pre>\n<p>\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u5c42\u7ea7\u5173\u7cfb\uff0c\u4f8b\u5982\u5728index\u8def\u5f84\u4e0b\u9762\u7684xxx\u8def\u5f84\u9700\u8981\u5230index\u7684\u5b50app<\/p>\n<p>\u5408\u7406\u7684\u4f5c\u6cd5\u662f<\/p>\n<pre><code class=\"language-python\" lang=\"python\">from django.urls import path\nfrom django.conf.urls import url, include\nfrom rest_framework_jwt.views import obtain_jwt_token\n\nurlpatterns = [\n    url(r'^login\/', obtain_jwt_token),\n    url(r'^', include('index.urls')),\n]\n\n<\/code><\/pre>\n<p>django\u7684include\u4f1a\u5f15\u5bfc\u8fd9\u4e2aapp<\/p>\n<p>\u6b64\u65f6\u518d\u5728index\u7684urls.py\u4e2d\u5efa\u7acb\u76f8\u5173\u7684\u5f15\u5bfc\u5373\u53ef<\/p>\n<pre><code class=\"language-python\" lang=\"python\">from django.urls import path\nfrom . import views\nurlpatterns = [\n    path('index\/', views.Index.as_view(), name='index'),\n    url(r'^index\/xxx\/', include('index.xxx.urls'))\t#\u5d4c\u5957\n]\n<\/code><\/pre>\n<ol start=\"5\">\n<li>\u5168\u5c40\u7684\u8ba4\u8bc1\u8bbe\u7f6e<\/li>\n<\/ol>\n<p>\u6839\u636e\u7b2c\u4e09\u8282\u7684\u6559\u7a0b\uff0c\u6211\u4eec\u9700\u8981\u5728\u6bcf\u4e2aview\u4e2d\u8bbe\u7f6e\u8ba4\u8bc1\uff0c\u4e00\u4e2a\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u8fdb\u884c\u5168\u5c40\u7684\u8bbe\u7f6e\uff0c\u5728setting.py\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\" lang=\"python\">REST_FRAMEWORK = {\n    'DEFAULT_AUTHENTICATION_CLASSES':(\n        'rest_framework.authentication.BasicAuthentication',\n        'rest_framework.authentication.SessionAuthentication',\n      \t\"rest_framework_jwt.authentication.JSONWebTokenAuthentication\",\n        \"index.jwtMiddleware.TokenAuth\"\n    )\n}\n<\/code><\/pre>\n<p>\u5c06\u9700\u8981\u7684\u8ba4\u8bc1\u7c7b\"index.jwtMiddleware.TokenAuth\"\u6dfb\u52a0\u5230\u4e0b\u9762\u5373\u53ef<\/p>\n<ol start=\"6\">\n<li>\u8ba4\u8bc1\u7c7b\u7684\u7528\u6237\u4fe1\u606f<\/li>\n<\/ol>\n<p>\u6839\u636e\u4e4b\u524d\u7684\u6559\u7a0b\u6211\u4eec\u521b\u5efa\u4e86\u8ba4\u8bc1\u7c7b\uff0c\u5bf9\u4e8erequest.user\u7684\u8fd4\u56de\u6559\u7a0b\u6ca1\u6709\u505a\uff0c\u6dfb\u52a0user\u548ctoken\u7684\u8fd4\u56de\u5373\u53ef<\/p>\n<p>\u5b8c\u6574\u7684\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\" lang=\"python\">from rest_framework.exceptions import AuthenticationFailed\nfrom rest_framework_jwt.serializers import VerifyJSONWebTokenSerializer\n\n\nclass TokenAuth:\n    def authenticate(self, request):\n        token = {\"token\": request.META.get('HTTP_TOKEN')}\n        # print(request.META.get(\"HTTP_TOKEN\"))\n        valid_data = VerifyJSONWebTokenSerializer().validate(token)\n        print(valid_data)\n        user = valid_data['user']\n        print(user)\n        if user:\n            return user, token\n        else:\n            raise AuthenticationFailed('\u8ba4\u8bc1\u5931\u8d25')\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u505a\u8fc7\u4e00\u4e9bDjango \u7684\u6a21\u5757\u5f00\u53d1\uff0c\u6bd5\u8bbe\u4f1a\u4ece\u5934\u53bb\u505a\u4e00\u4e2a\u540e\u7aef\uff0c\u6240\u4ee5\u8bb0\u5f55\u4e00\u4e0b\u6700\u5f00\u59cb\u6784\u5efa\u7684\u5751 \u6570\u636e\u5e93\u8fde\u63a5 \u5f53\u4f60\u6b63\u5e38\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff08\u5373st &#8230;<\/p>","protected":false},"author":1,"featured_media":81,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-8"],"_links":{"self":[{"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":2,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions\/86"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.maoyulong.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}