django_rest_swagger - 'staticfiles' 不是注册的标签库。必须是以下之一:

Luc*_*cas 11 python django swagger django-rest-swagger

尝试在 django 2.2.4 中为 django rest 框架自动生成 API 文档时出现此错误,从我在日志中看到的内容来看,它与 {% load staticfiles %} 的弃用有关

templateSyntaxError at /
'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
countries
i18n
l10n
log
phone
rest_framework
static
tz
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 3.0
Exception Type: TemplateSyntaxError
Exception Value:    
'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
countries
i18n
l10n
log
phone
rest_framework
static
tz

{% load i18n %}
2   {% load staticfiles %}
3   <!DOCTYPE html>
4   <html lang="en">
5   <head>
6     <meta charset="UTF-8">
7     <title>Swagger UI</title>
8     <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
9     <link href="{% static 'rest_framework_swagger/bundles/vendors.bundle.css' %}" rel="stylesheet" type="text/css">
10    <link href="{% static 'rest_framework_swagger/bundles/app.bundle.css' %}" rel="stylesheet" type="text/css">
11    {% block extra_styles %}
12    {# -- Add any additional CSS scripts here -- #}
Run Code Online (Sandbox Code Playgroud)

我的 urls 文件的格式与 django_rest_swagger 文档中的格式相同:

from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view


    schema_view = get_swagger_view(title='My API')

        urlpatterns = [
            path('admin/', admin.site.urls),
            path('api/user/', include('user.urls'),
            path('', schema_view)]
Run Code Online (Sandbox Code Playgroud)

Mus*_*cim 25

问题是staticfiles模板标签在 Django 2.2 中被弃用,最终在 Django 3.0 中被删除

djagno-rest-swagger包本身已被弃用,不再维持。

他们的GitHub repo推荐了类似的东西drf-yasg


小智 16

这是开发人员代码中的一个错误,位于 site-packages/rest_framework_swagger/templates/rest_framework_swagger/index.html

{% load staticfiles %} 行(第 2 行)应为 {% load static %}。您可以手动编辑它


Jav*_*vad 9

您可以通过将以下代码片段添加到 settings.py 中的 TEMPLATES 来解决此问题:

    'libraries': {  
                    'staticfiles': 'django.templatetags.static',
                 },
Run Code Online (Sandbox Code Playgroud)

像这样


naf*_*fiu 5

您还可以staticfiles.pytemplatetags应用程序的文件夹中创建一个,然后粘贴以下代码:

from django import template
from django.templatetags.static import (do_static as _do_static,static as _static,)

register = template.Library()

def static(path):
    return _static(path)

@register.tag('static')
def do_static(parser, token):
    return _do_static(parser, token)'
Run Code Online (Sandbox Code Playgroud)

进入文件。

但请确保您的应用程序出现在rest_framework_swagger您的settings.py.