如何在Django REST框架中使用Swagger API
WEB前端开发社区 今天
<span class="code-snippet_outer">django-admin startproject djangomysqlrestcrudswagger</span>
<span class="code-snippet_outer">django-admin startapp restcrudswagger</span>
<span class="code-snippet_outer"><span class="code-snippet__attribute">INSTALLED_APPS</span> = [</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__string">'rest_framework_swagger'</span>,</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__string">'restcrudswagger.apps.RestcrudswaggerConfig'</span>,</span></code><code><span class="code-snippet_outer">]</span>
<span class="code-snippet_outer"><span class="code-snippet__attr">REST_FRAMEWORK</span> = { <span class="code-snippet__string">'DEFAULT_SCHEMA_CLASS'</span>: <span class="code-snippet__string">'rest_framework.schemas.coreapi.AutoSchema'</span> }</span>
<span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> rest_framework_swagger.views <span class="code-snippet__keyword">import</span> get_swagger_view</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">schema_view = get_swagger_view(title=<span class="code-snippet__string">'User API'</span>)</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">urlpatterns = [</span></code><code><span class="code-snippet_outer"> url(<span class="code-snippet__string">'^$'</span>, schema_view)</span></code><code><span class="code-snippet_outer">]</span>
<span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> django.urls <span class="code-snippet__keyword">import</span> include, path</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> rest_framework <span class="code-snippet__keyword">import</span> routers</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> restcrudswagger <span class="code-snippet__keyword">import</span> views</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> django.conf.urls <span class="code-snippet__keyword">import</span> url</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">from</span> rest_framework_swagger.views <span class="code-snippet__keyword">import</span> get_swagger_view</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">router = routers.DefaultRouter()</span></code><code><span class="code-snippet_outer">router.register(<span class="code-snippet__string">r'users'</span>, views.UserViewSet)</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">schema_view = get_swagger_view(title=<span class="code-snippet__string">'User API'</span>)</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">urlpatterns = [</span></code><code><span class="code-snippet_outer"> url(<span class="code-snippet__string">'^$'</span>, schema_view),</span></code><code><span class="code-snippet_outer"> url(<span class="code-snippet__string">r'^'</span>, include(router.urls))</span></code><code><span class="code-snippet_outer">]</span>
<span class="code-snippet_outer"><span class="code-snippet__meta"><!DOCTYPE html></span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag"><<span class="code-snippet__name">html</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">head</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">title</span>></span>Swagger<span class="code-snippet__tag"></<span class="code-snippet__name">title</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">meta</span> <span class="code-snippet__attr">charset</span>=<span class="code-snippet__string">"utf-8"</span>/></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">meta</span> <span class="code-snippet__attr">name</span>=<span class="code-snippet__string">"viewport"</span> <span class="code-snippet__attr">content</span>=<span class="code-snippet__string">"width=device-width, initial-scale=1"</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">link</span> <span class="code-snippet__attr">rel</span>=<span class="code-snippet__string">"stylesheet"</span> <span class="code-snippet__attr">type</span>=<span class="code-snippet__string">"text/css"</span> <span class="code-snippet__attr">href</span>=<span class="code-snippet__string">"//unpkg.com/swagger-ui-dist@3/swagger-ui.css"</span> /></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"></<span class="code-snippet__name">head</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">body</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">div</span> <span class="code-snippet__attr">id</span>=<span class="code-snippet__string">"swagger-ui"</span>></span><span class="code-snippet__tag"></<span class="code-snippet__name">div</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">script</span> <span class="code-snippet__attr">src</span>=<span class="code-snippet__string">"//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"</span>></span><span class="code-snippet__tag"></<span class="code-snippet__name">script</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"><<span class="code-snippet__name">script</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">const</span> ui = SwaggerUIBundle({</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">url</span>: <span class="code-snippet__string">"{% url schema_url %}"</span>,</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">dom_id</span>: <span class="code-snippet__string">'#swagger-ui'</span>,</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">presets</span>: [</span></code><code><span class="code-snippet_outer"> SwaggerUIBundle.presets.apis,</span></code><code><span class="code-snippet_outer"> SwaggerUIBundle.SwaggerUIStandalonePreset</span></code><code><span class="code-snippet_outer"> ],</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">layout</span>: <span class="code-snippet__string">"BaseLayout"</span></span></code><code><span class="code-snippet_outer"> })</span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag"></<span class="code-snippet__name">script</span>></span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__tag"></<span class="code-snippet__name">body</span>></span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__tag"></<span class="code-snippet__name">html</span>></span></span>
来源:
https://www.roytuts.com/how-to-use-swagger-with-python-based-django-rest-apis/
赞 (0)