# 02.基于View类的视图
[toc]{type: "ol", level: [2, 3, 4, 5]}
### 创建路由
```python
### shop.urls
# 基于View类的视图
path('category/list/view/', CategoryListView.as_view(), name='categoryListView'),
re_path(r'^category/detail/view/(\d+)/$', CategoryDetailView.as_view(), name='categoryDetailView'),
```
<br><br>
### 创建视图
#### 导入模块
```python
### shop.views
from django.views import View
from django.http import HttpResponse
```
#### 多个对象
```python
### shop.views
class CategoryListView(View):
# 重写对应的Http方法
def get(self, request):
# WSGIRequest
return HttpResponse('获取列表成功')
def post(self, request):
return HttpResponse('创建对象成功')
```
#### 单个对象
```python
### shop.views
class CategoryDetailView(View):
def get(self, request, cid):
return HttpResponse('获取对象成功')
def put(self, request, cid):
return HttpResponse('获取修改成功')
def patch(self, request, cid):
return HttpResponse('获取修改成功')
def delete(self, request, cid):
return HttpResponse('删除对象成功')
```