class UsersObjectListMixin(View):
"""封装返回列表页数据"""
model = None
def get(self, request):
response = {}
try:
queryset = self.model.objects.all()
response['list'] = json.loads(serializers.serialize('json', queryset))
response['status'] = 'success'
response['error_num'] = 0
except Exception as e:
response['status'] = str(e)
response['error_num'] = 1
"""封装返回列表页数据"""
model = None
def get(self, request):
response = {}
try:
queryset = self.model.objects.all()
response['list'] = json.loads(serializers.serialize('json', queryset))
response['status'] = 'success'
response['error_num'] = 0
except Exception as e:
response['status'] = str(e)
response['error_num'] = 1
return JsonResponse(response)
class GroupCreate(UsersObjectListMixin):
"""
创建角色
"""
model = Group
def get(self, request):
return super(GroupCreate, self).get(request)
def post(self, request):
form = GroupForm(request.POST)
if form.is_valid():
name = request.POST.get('name', '')
mygroup = Group.objects.create(
name=name
)
return JsonResponse({'status':'success'})
else:
return JsonResponse({"status": "fail", "msg": form.errors})