12

尝试在 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 -- #}

我的 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)]
4

9 回答 9

25

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

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

他们的GitHub 存储库推荐了类似的东西drf-yasg

于 2019-12-18T22:45:26.057 回答
16

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

    'libraries': {  
                    'staticfiles': 'django.templatetags.static',
                 },

像这样

于 2021-02-22T14:00:39.323 回答
16

该库已弃用

快速修复(风险自负):转到 site-packages/rest_framework_swagger/templates/rest_framework_swagger/index.html

带有 {% load staticfiles %} 的行(第 2 行)应该是 {% load static %}。您可以手动编辑它

于 2019-12-16T21:43:03.553 回答
6

您还可以在应用程序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)'

到文件中。

但请确保您rest_framework_swagger的应用在您的settings.py.

于 2020-06-09T17:46:55.570 回答
2

staticfiles 模板在 Django 2.2 中已弃用,并在 Django 3 中删除。

我们需要覆盖rest_framework_swagger/index.html.

site-packages/rest_framework_swagger/templates/rest_framework_swagger

复制 rest_framework_swagger 文件夹并将其粘贴到您自己的模板文件夹中

然后用{% load static %}index.html 中的第 2 行替换

希望它有效!

于 2020-03-15T07:44:45.707 回答
1

我发现 django-rest-swagger 模块在 django 3.0 或更高版本中不可用,因为模板静态标签已被删除。

请使用其他 swagger 包,例如drf-yasg

于 2020-07-27T20:43:55.110 回答
0

要解决这个问题:

  • 导航到 rest_framework_swagger 的安装路径,对我来说是env\Lib\site-packages\rest_framework_swagger
  • 转到templates/rest_framework_swagger文件夹并更改staticfilestatic您的 index.html

这是因为 {% load staticfiles %} 和 {% load admin_static %} 在 Django 2.1 中已被弃用,并在 Django 3.0 中被删除。

在我们在 rest_framework_swagger 应用程序中更新它之前,您可以使用这个变通解决方案。

于 2020-07-28T18:31:00.850 回答
0

django-rest-framework 正在使用 Django 2 中使用的 Template Tag staticfiles。它在 Django 3.0 中被删除,而 django-rest-frame repo 不在更新时。它在 2019 年被弃用。我建议使用https://github.com/axnsan12/drf-yasg中的 drf-yasg

它执行类似的功能,使用交换器自动提供 API 文档。我已经使用它,它似乎足够好。

于 2021-02-18T13:16:50.500 回答
0

https://github.com/axnsan12/drf-yasg#quickstart

在 settings.py 中:

INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles',  # required for serving swagger ui's css/js files
   ...
]
于 2021-05-25T19:12:01.663 回答