django rest framework 简称drf
https://www.django-rest-framework.org/
安装之前
安装
pip install django-guardian
安装
pip install coreapi
安装两个之后
启动报错
"C:\Program Files\JetBrains\PyCharm 2018.2.4\bin\runnerw.exe" D:\Envs\MxShop\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 53863 --file C:/Users/huang/PycharmProjects/MxShop/manage.py runserver 8000
pydev debugger: process 18268 is connecting
Connected to pydev debugger (build 182.4505.26)
pydev debugger: process 11984 is connecting
Performing system checks...
Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x000001A320B19E80>
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 614, in __call__
return self.original_func(*self.args, **self.kwargs)
File "D:\Envs\MxShop\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "D:\Envs\MxShop\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "D:\Envs\MxShop\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "D:\Envs\MxShop\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "D:\Envs\MxShop\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\Envs\MxShop\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "D:\Envs\MxShop\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "D:\Envs\MxShop\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "D:\Envs\MxShop\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Envs\MxShop\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Envs\MxShop\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Envs\MxShop\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\huang\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\huang\PycharmProjects\MxShop\MxShop\urls.py", line 35, in <module>
url(r'docs/', include_docs_urls(title="木星"))
File "D:\Envs\MxShop\lib\site-packages\rest_framework\documentation.py", line 68, in include_docs_urls
permission_classes=permission_classes,
File "D:\Envs\MxShop\lib\site-packages\rest_framework\documentation.py", line 29, in get_docs_view
permission_classes=permission_classes,
File "D:\Envs\MxShop\lib\site-packages\rest_framework\schemas\__init__.py", line 41, in get_schema_view
urlconf=urlconf, patterns=patterns,
File "D:\Envs\MxShop\lib\site-packages\rest_framework\schemas\generators.py", line 257, in __init__
assert coreapi, '`coreapi` must be installed for schema support.'
AssertionError: `coreapi` must be installed for schema support.
Process finished with exit code 0
难道是我环境搞错了
进入 这个环境
安装两个
正常启动
访问报错:http://127.0.0.1:8000/docs/
"C:\Program Files\JetBrains\PyCharm 2018.2.4\bin\runnerw.exe" D:\Envs\MxShop\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 54011 --file C:/Users/huang/PycharmProjects/MxShop/manage.py runserver 8000
pydev debugger: process 5496 is connecting
Connected to pydev debugger (build 182.4505.26)
pydev debugger: process 7088 is connecting
Performing system checks...
System check identified no issues (0 silenced).
April 23, 2019 - 23:06:28
Django version 1.11, using settings 'MxShop.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[23/Apr/2019 23:06:36] "GET /goods/ HTTP/1.1" 200 8809
[23/Apr/2019 23:06:40] "GET /docs HTTP/1.1" 301 0
Internal Server Error: /docs/
Traceback (most recent call last):
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 77, in wrap_attributeerrors
yield
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 223, in user
self._authenticate()
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 376, in _authenticate
user_auth_tuple = authenticator.authenticate(self)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\authentication.py", line 129, in authenticate
self.enforce_csrf(request)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\authentication.py", line 140, in enforce_csrf
check.process_request(request)
AttributeError: 'CSRFCheck' object has no attribute 'process_request'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Envs\MxShop\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\Envs\MxShop\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Envs\MxShop\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Envs\MxShop\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "D:\Envs\MxShop\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\schemas\views.py", line 41, in handle_exception
return super(SchemaView, self).handle_exception(exc)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\views.py", line 483, in dispatch
self.initial(request, *args, **kwargs)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\views.py", line 400, in initial
self.perform_authentication(request)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\views.py", line 326, in perform_authentication
request.user
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 223, in user
self._authenticate()
File "C:\Users\huang\AppData\Local\Programs\Python\Python36\lib\contextlib.py", line 100, in __exit__
self.gen.throw(type, value, traceback)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 81, in wrap_attributeerrors
six.reraise(type(exc), exc, info[2])
File "D:\Envs\MxShop\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 77, in wrap_attributeerrors
yield
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 223, in user
self._authenticate()
File "D:\Envs\MxShop\lib\site-packages\rest_framework\request.py", line 376, in _authenticate
user_auth_tuple = authenticator.authenticate(self)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\authentication.py", line 129, in authenticate
self.enforce_csrf(request)
File "D:\Envs\MxShop\lib\site-packages\rest_framework\authentication.py", line 140, in enforce_csrf
check.process_request(request)
rest_framework.request.WrappedAttributeError: 'CSRFCheck' object has no attribute 'process_request'
[23/Apr/2019 23:06:40] "GET /docs/ HTTP/1.1" 500 156043
把
'rest_framework',
配置到 settings.py
拷贝过来
1.
2.
新建文件
C:\Users\huang\PycharmProjects\MxShop\apps\goods\serializers.py
------------------第二段视频了
Creating a Serializer class
C:\Users\huang\PycharmProjects\MxShop\apps\goods\serializers.py
from rest_framework import serializers
class GoodsSerializer(serializers.Serializer):
name = serializers.CharField(required=True, max_length=100)
click_num = serializers.IntegerField(default=0)
C:\Users\huang\PycharmProjects\MxShop\apps\goods\views.py
from .serializers import GoodsSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Goods
# Create your views here.
class GoodsListView(APIView):
"""
List all snippets, or create a new snippet.
"""
def get(self, request, format=None):
goods = Goods.objects.all()[:10]
goods_serializer = GoodsSerializer(goods, many=True)
return Response(goods_serializer.data)
修改为下面一行
启动正常
访问:http://127.0.0.1:8000/goods/
报错:
WrappedAttributeError at /goods/
'CSRFCheck' object has no attribute 'process_request'
Request Method: GET
Request URL: http://127.0.0.1:8000/goods/
Django Version: 1.11
Exception Type: WrappedAttributeError
Exception Value:
'CSRFCheck' object has no attribute 'process_request'
Exception Location: D:\Envs\MxShop\lib\site-packages\rest_framework\authentication.py in enforce_csrf, line 140
Python Executable: D:\Envs\MxShop\Scripts\python.exe
Python Version: 3.6.0
Python Path:
['C:\\Users\\huang\\PycharmProjects\\MxShop\\extra_apps',
'C:\\Users\\huang\\PycharmProjects\\MxShop\\apps',
'C:\\Users\\huang\\PycharmProjects\\MxShop',
'C:\\Users\\huang\\PycharmProjects\\MxShop',
'C:\\Program Files\\JetBrains\\PyCharm 2018.2.4\\helpers\\pydev',
'C:\\Users\\huang\\PycharmProjects\\MxShop',
'C:\\Users\\huang\\PycharmProjects\\MxShop\\apps',
'C:\\Users\\huang\\PycharmProjects\\MxShop\\extra_apps',
'C:\\Program Files\\JetBrains\\PyCharm 2018.2.4\\helpers\\pydev',
'C:\\Users\\huang\\.PyCharm2018.2\\system\\cythonExtensions',
'D:\\Envs\\MxShop\\Scripts\\python36.zip',
'C:\\Users\\huang\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\huang\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\huang\\AppData\\Local\\Programs\\Python\\Python36',
'D:\\Envs\\MxShop',
'D:\\Envs\\MxShop\\lib\\site-packages',
'D:\\Envs\\MxShop\\lib\\site-packages\\setuptools-39.1.0-py3.6.egg',
'C:\\Program Files\\JetBrains\\PyCharm '
'2018.2.4\\helpers\\pycharm_matplotlib_backend']
Server time: 星期二, 23 四月 2019 23:51:39 +0800
把 django 1.11 改成1.11.6之后
启动
访问又报错:
__str__ returned non-string (type NoneType)
49 | {% optional_logout request user %} |
---|
解决办法:
重新访问:
下面多显示一个图片字段
goods_front_image = serializers.ImageField()
启动访问