简体   繁体   中英

django_rest_swagger - 'staticfiles' is not a registered tag library. Must be one of:

Have this error while trying to autogenerate API docs for django rest framework in django 2.2.4, from what I'm seeing in the logs it has something to do with the deprectation of the {% 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 -- #}

My urls file, it has the same format that in the django_rest_swagger documentation:

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)]

The problem is that staticfiles template tag was deprecated in Django 2.2 and is finally removed in Django 3.0

The djagno-rest-swagger package itself is deprecated and is no longer maintained.

Their GitHub repo recommends something like drf-yasg

It's a bug in the developer's code, in site-packages/rest_framework_swagger/templates/rest_framework_swagger/index.html

The line with {% load staticfiles %} (line 2) should be {% load static %}. You can edit it manually

You can solve this issue by adding the following code snippet to TEMPLATES in settings.py:

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

Like this

You can also create a staticfiles.py in the templatetags folder of your app and then paste this code:

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)'

into the file.

But make sure that the your app comes before rest_framework_swagger in the app list of your settings.py .

The staticfiles template is deprecated in Django 2.2 and removed in Django 3.

We need to override the rest_framework_swagger/index.html .

Go to site-packages/rest_framework_swagger/templates/rest_framework_swagger

Copy the rest_framework_swagger folder and paste it in your own templates folder

Then replace the line 2 with {% load static %} in index.html

Hope it works !!

I found out that the django-rest-swagger Module is not available in django 3.0 or more since the template static label is removed.

Please use other swagger packages, such as drf-yasg

To get around this:

  • navigate to your installation path of rest_framework_swagger, for me it's env\\Lib\\site-packages\\rest_framework_swagger
  • goto templates/rest_framework_swagger folder and change staticfile to static in your index.html

This is because {% load staticfiles %} and {% load admin_static %} were deprecated in Django 2.1, and removed in Django 3.0.

This work around solution is what you can use till we get it updated in rest_framework_swagger app.

The django-rest-framework is using the Template Tag staticfiles which was used in Django 2. It was been dropped in Django 3.0, while the django-rest-frame repo is not while update. It was deprecated in 2019. I whould suggest using drf-yasg from https://github.com/axnsan12/drf-yasg

It preforms a similar function, to provide API docs automatically using swapper. I've used it and it seems good enough.

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

In settings.py:

INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles',  # required for serving swagger ui's css/js files
   ...
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM