无法使用django rest framework

来源:5-5 apiview方式实现商品列表页-2

weixin_慕斯1066307

2023-03-24

老师你好,我一旦import drf 就会报以下错,找了很久也没找到解决方案,drf版本是3.13.1, django版本是2.2,maekdown版本为3.2, python为3.6,coreapi是2.3.3,已经查看了官方文档没发现有版本不适配的问题,麻烦老师帮忙看一下

Watching for file changes with StatReloader
Traceback (most recent call last):
  File "/python36/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2191, in _handle_ns
    loader = importer.find_spec(str(packageName)).loader
  File "<frozen importlib._bootstrap_external>", line 1268, in find_spec
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/python36/lib/python3.6/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/python36/lib/python3.6/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/python36/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/python36/lib/python3.6/site-packages/rest_framework/templatetags/rest_framework.py", line 11, in <module>
    from rest_framework.compat import apply_markdown, pygments_highlight
ImportError: cannot import name 'apply_markdown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/python36/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/python36/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/python36/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/python36/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/python36/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/python36/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/python36/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/python36/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 80, in check_dependencies
    for engine in engines.all():
  File "/python36/lib/python3.6/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/python36/lib/python3.6/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/python36/lib/python3.6/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/python36/lib/python3.6/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/python36/lib/python3.6/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/python36/lib/python3.6/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/python36/lib/python3.6/site-packages/django/template/backends/django.py", line 125, in get_package_libraries
    "trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'rest_framework.templatetags.rest_framework': cannot import name 'apply_markdown'

  File "<frozen importlib._bootstrap_external>", line 60, in _path_join
  File "<frozen importlib._bootstrap_external>", line 60, in <listcomp>
AttributeError: 'PosixPath' object has no attribute 'rstrip'

写回答

1回答

bobby

2023-03-24

drf版本改为 

3.12.0

试试

0
2
bobby
回复
weixin_慕斯1066307
https://blog.csdn.net/qq_35889860/article/details/113757296 看看这个
2023-03-26
共2条回复

Python前后端分离开发Vue+Django REST framework实战

Django REST framework课程视频,RESTFul API前后端分离开发

2873 学习 · 2457 问题

查看课程