python 首页轮播图后台实现

views.py文件

class NoticeO:
class NoticeViewSets(viewsets.ModelViewSet):
# queryset = Notice.objects.all().order_by('-id')
queryset = Notice.objects.all().order_by('-pic_mark')
serializer_class = serializers.NoticeSerializer
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
filter_fields = ('id', 'title')
search_fields = ('$c_job_name',)
class NoticeInfo(views.APIView):
# 获得
def get(self, request, info):
if info not in ['fontnotice', 'noticedetail', 'delete']:
return JsonResponse({'statusCode': STATUES_CODES['URL_ERROR'], 'message': '路由错误'})
#获得图片
if info == 'fontnotice':
notices = Notice.objects.filter(public=True).values('id', 'pic_mark', 'pic_front', 'title', 'content')
notices = list(notices)
# 按照pic_mark进行排序
notices = sorted(notices, key=lambda i: i['pic_mark'], reverse=False)
return JsonResponse({'statusCode': STATUES_CODES['OK'], 'message': '成功', 'data': notices})
if info == 'noticedetail':
id = request.GET.get('id')
notices = Notice.objects.filter(id=id).values('id', 'pic_front', 'title', 'content', 'pic_background',
'public')
return JsonResponse({'statusCode': STATUES_CODES['OK'], 'message': '成功', 'data': list(notices)[0]})
# 删除图片
if info == 'delete':
id = request.GET.get('id')
if id:
notice = Notice.objects.get(id=id)
notice.delete()
return JsonResponse({'ok': 'ok'})
else:
return JsonResponse({'statusCode': STATUES_CODES['PARAM_ERROR'], 'message': '数据不存在'})

    # 新增、修改
    def post(self, request, info):
        if info not in ['create', 'change']:
            return JsonResponse({'statusCode': STATUES_CODES['URL_ERROR'], 'message': '路由错误'})
        id = request.POST.get('id', '')
        image_font = request.FILES.get('image_font', '')
        image_background = request.FILES.get('image_back', '')
        title = request.POST.get('file', '')
        content = request.POST.get('content', '')
        public = request.POST.get('is_active', '')
        image_mark = request.POST.get('image_mark', '')  # 前端传过来的排序字符串
        tag_id = request.POST.get('tag_id', '')  # 排好顺序的字符串
        print(tag_id)
       

        #保存到fastdfs里
        if image_font:
            image_font_path = 'rdsystem/files/' + str(uuid.uuid1()) + str(uuid.uuid4()) + \
                              os.path.splitext(image_font._name)[1]
            with open(image_font_path, 'wb') as file:
                file.write(image_font.read())
            image_font_path_upload = FastDFSStorage().upload(image_font_path)[1]
            os.remove(image_font_path)
        if image_background:
            image_background_path = 'rdsystem/files/' + str(uuid.uuid1()) + str(uuid.uuid4()) + \
                                    os.path.splitext(image_font._name)[1]
            with open(image_background_path, 'wb') as file:
                file.write(image_background.read())
            image_background_path_upload = FastDFSStorage().upload(image_background_path)[1]
            os.remove(image_background_path)
        #增加
        if info == 'create':
            notice = Notice.objects.create(content=content, public=False, title=title)

            if public:
                notice.public = eval(public)
            if image_font and image_font_path_upload:
                notice.pic_front = image_font_path_upload
                print(notice.pic_front)
         
            if image_background and image_background_path_upload:
                notice.pic_background = image_background_path_upload

            result = {}
            result['data'] = []
            print(id)
            result['data'].append({'id': notice.id,
                                   'image_font': notice.pic_front})
            result['errorCode'] = STATUES_CODES['OK']
            result['message'] = 'ok'
            notice.save()
            print(result)
            return JsonResponse(result)

            # notice.save()

        # if info == 'change':
        #     # print('112323123')
        #     try:
        #         notice = Notice.objects.get(id=id)
        #         notice.title = title
        #         notice.content = content
        #         notice.save()
        #     except:
        #         return JsonResponse({'statusCode': STATUES_CODES['PARAM_ERROR'], 'message': '请检查你传入的id'})
        # if public:
        #     notice.public = eval(public)
        # if image_font and image_font_path_upload:
        #     notice.pic_front = image_font_path_upload
        #     print(notice.pic_front)
        # if image_background and image_background_path_upload:
        #     notice.pic_background = image_background_path_upload
        # notice.save()
        if info == 'change':
            # 得到一个图片排好顺序的字符串,形如tag_id='1,2,3',并更新排序字段pic_mark
            result = {}
            tag_id_list = tag_id.split(',')
            for id in tag_id_list:
                sort_number = str(tag_id_list.index(id) + 1)
                Notice.objects.filter(id=id).update(pic_mark=sort_number)
                result['errorCode'] = STATUES_CODES['OK']
                result['message'] = '修改成功!'
            return JsonResponse(result)

model.py文件

class Notice(models.Model):
title=models.CharField(u'标题',max_length=100)
content=models.TextField(u'公告内容')
pic_front=models.TextField(u'轮播图片地址',max_length=100,null=True,blank=True)
pic_background=models.CharField(u'公告背景图地址',max_length=100,null=True,blank=True)
public=models.BooleanField(u'公告展示',default=False)
pic_mark=models.CharField(u'标记内容',max_length=100,null=True,blank=True)

class Meta:
    verbose_name=u'公告'
    verbose_name_plural = verbose_name
    db_table='t_notice'

url.py文件

from django.conf.urls import url, include
urlpatterns = [
url(r'^operatenotice/(?P\S+)/$',views.NoticeO.NoticeInfo.as_view()),
]

总url文件
url.py文件

from django.conf.urls import url, include
urlpatterns = [
url(r'^rdsystem/', include('rdsystem.urls', namespace='rdsystem')),
]

posted @ 2019-09-10 19:27  温温温  阅读(762)  评论(0)    收藏  举报