我有一个使用 Django 和 Django Rest Framework 的 Web 应用程序很多年了。该 API 由 python 应用程序使用并且工作正常:
import requests
response = requests.options(http://example.com/api/albums/)
200
{'name': 'Album List List', 'description': '', 'renders': ['application/json', 'text/html'], 'parses': ['application/json', 'application/x-www-form-urlencoded', 'multipart/form-data'], 'actions': {'POST': ....
我发现了 Svelte,我想使用这个 API,但不可能:
let api_ = "http://example.com/api/albums/"
const getAlbums = async () => {
var response = await fetch(api, {
mode: 'cors', method: 'GET', headers: {
'Content-Type': 'application/json'}});
var result = await response.json();
return result.results;
Cross-Origin Request Blocked: The “Same Origin” policy does not allow viewing the remote resource located at http://example.com/api/albums/. Reason: The “Access-Control-Allow-Origin” CORS header is missing. Status Code: 200.
我读到您需要安装 django-cors-headers。但现在我有这个错误:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 69, in __getitem__
return self._engines[alias]
KeyError: 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 174, in get_package_libraries
module = import_module(entry[1])
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/rest_framework/templatetags/rest_framework.py", line 12, in <module>
from rest_framework.renderers import HTMLFormRenderer
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/rest_framework/renderers.py", line 17, in <module>
from django.http.multipartparser import parse_header
ImportError: cannot import name 'parse_header' from 'django.http.multipartparser' (/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/http/multipartparser.py)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
Traceback (most recent call last):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 69, in __getitem__
return self._engines[alias]
KeyError: 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 174, in get_package_libraries
self._target(*self._args, **self._kwargs)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
module = import_module(entry[1])
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
fn(*args, **kwargs)
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
self.check(display_num_errors=True)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/base.py", line 486, in check
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
all_issues = checks.run_checks(
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/rest_framework/templatetags/rest_framework.py", line 12, in <module>
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/checks/templates.py", line 10, in check_templates
from rest_framework.renderers import HTMLFormRenderer
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/rest_framework/renderers.py", line 17, in <module>
for engine in engines.all():
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 94, in all
from django.http.multipartparser import parse_header
ImportError: cannot import name 'parse_header' from 'django.http.multipartparser' (/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/http/multipartparser.py)
return [self[alias] for alias in self]
The above exception was the direct cause of the following exception:
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 94, in <listcomp>
Traceback (most recent call last):
File "/home/vianney/Documents/django_blog/manage.py", line 22, in <module>
return [self[alias] for alias in self]
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 85, in __getitem__
main()
File "/home/vianney/Documents/django_blog/manage.py", line 18, in main
engine = engine_cls(params)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 26, in __init__
execute_from_command_line(sys.argv)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
options["libraries"] = self.get_templatetag_libraries(libraries)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 88, in get_templatetag_libraries
utility.execute()
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
libraries = get_installed_libraries()
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 162, in get_installed_libraries
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
return {
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 162, in <dictcomp>
self.execute(*args, **cmd_options)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 75, in execute
return {
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 151, in get_template_tag_modules
super().execute(*args, **options)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
for name in get_package_libraries(pkg):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 176, in get_package_libraries
output = self.handle(*args, **options)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 112, in handle
raise InvalidTemplateLibrary(
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'rest_framework.templatetags.rest_framework': cannot import name 'parse_header' from 'django.http.multipartparser' (/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/http/multipartparser.py)
self.run(**options)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 119, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/utils/autoreload.py", line 671, in run_with_reloader
start_django(reloader, main_func, *args, **kwargs)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/utils/autoreload.py", line 660, in start_django
reloader.run(django_main_thread)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/utils/autoreload.py", line 343, in run
autoreload_started.send(sender=self)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 189, in send
response = receiver(signal=self, sender=sender, **named)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/autoreload.py", line 50, in watch_for_template_changes
for directory in get_template_directories():
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/autoreload.py", line 16, in get_template_directories
for backend in engines.all():
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 94, in all
return [self[alias] for alias in self]
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 94, in <listcomp>
return [self[alias] for alias in self]
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/utils.py", line 85, in __getitem__
engine = engine_cls(params)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 26, in __init__
options["libraries"] = self.get_templatetag_libraries(libraries)
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 88, in get_templatetag_libraries
libraries = get_installed_libraries()
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 162, in get_installed_libraries
return {
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 162, in <dictcomp>
return {
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 151, in get_template_tag_modules
for name in get_package_libraries(pkg):
File "/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/template/backends/django.py", line 176, in get_package_libraries
raise InvalidTemplateLibrary(
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'rest_framework.templatetags.rest_framework': cannot import name 'parse_header' from 'django.http.multipartparser' (/home/vianney/Documents/.blog/lib/python3.10/site-packages/django/http/multipartparser.py)
我的Python版本是3.10.12,Django是5.1.2,Rest Framework是3.12.4,我在firefox下安装了Cors Unlocked插件。
有人能解释一下吗?为什么使用 python 我可以访问我的 api 而不能使用 javascript?
谢谢您的帮助。
这个问题更接近于兼容性问题。确保您使用正确的 django 版本。例如这两个是兼容的 django==4.2.9 djangorestframework==3.14.0