How to handle to GET request in in same FBV(function based View.)

我与影子孤独终老i 提交于 2021-02-08 10:40:19

问题


Hello Everyone I am a beginner in Django and I have created one webpage where the user is able to search/Filter results and for this, I have used the Django-filter concept. Now my requirement is user should be able to download that filtered data. To achieve this I have created one separate View class with a download name and trying to pass the same Filter Class Queryset but with no luck, it is giving me all model data (records).

Please find the Post: how to use Django filtered class data to 2 seperate view

After this, I have checked by keeping the download view in the same render view and found that it is working. (Here problem is that both get requests and at a time only one is working.)

My requirement is after doing a search if a user is clicking on download then the second download logic should execute.

############### CTA #############

def retrievecta_view(request):
    if request.method == 'GET':
        allcta = CTA.objects.all()
        allcta1 = allcta
        allctagen = allcta1.filter(Shift_timing__exact='General')
        allctamor = allcta1.filter(Shift_timing__exact='Morning')
        allctseve = allcta1.filter(Shift_timing__exact='Evening')
        allctatotal = allcta1.filter(Shift_timing__exact='Total')

        # For filtering using   'django_filters',
        cta_list = CTA.objects.all()
        cta_filter = CTAFilter(request.GET, queryset=cta_list)
        allcta = cta_filter.qs

        paginator = Paginator(allcta, 50)
        page_number = request.GET.get('page')
        try:
            allcts = paginator.page(page_number)
        except PageNotAnInteger:
            allcts = paginator.page(1)
        except EmptyPage:
            allcts = paginator.page(paginator.num_pages)
        return render(request, 'abcd/cta.html', {'allcta': allcta, 'cta_filter': cta_filter, 'allcta1': allcta1,
                                                  'allctagen': allctagen, 'allctamor': allctamor,
                                                  'allctaeve': allctaeve,
                                                  'allctatotal': allctatotal})


        if request.method == 'GET':
            response = HttpResponse(content_type='application/ms-excel')
            response['Content-Disposition'] = 'attachment; filename="CTA_ShiftTiming.xls"'
            wb = xlwt.Workbook(encoding='utf-8')
            ws = wb.add_sheet('CTA_ShiftChange Data')  # this will make a sheet named Users Data
            # Sheet header, first row
            row_num = 0
            font_style = xlwt.XFStyle()
            font_style.font.bold = True
            columns = ['id', 'IDk', 'Shift_timing', 'EmailID', 'Vendor_Company', 'Project_name',
                       'SerialNumber',
                       'Reason', 'last_updated_time']
            for col_num in range(len(columns)):
                ws.write(row_num, col_num, columns[col_num], font_style)  # at 0 row 0 column
            # Sheet body, remaining rows
            font_style = xlwt.XFStyle()
            cta_list = CTA.objects.all()
            cta_filter = CTAFilter(request.GET, queryset=cta_list)
            allcta = cta_filter.qs
            print('All cta check in export:', allcta)
            rows = allcta.values_list('id', 'IDK', 'Shift_timing', 'EmailID', 'Vendor_Company',
                                      'Project_name', 'SerialNumber', 'Reason', 'last_updated_time')

            for row in rows:
                row_num += 1
                for col_num in range(len(row)):
                    ws.write(row_num, col_num, row[col_num], font_style)
            wb.save(response)
            return response

Can anyone help me how to write both the logic incorrect way so that after doing filter I i will click on download then second method should excecute.Any help or suggestion on this will be highly appreciated.

来源:https://stackoverflow.com/questions/65391528/how-to-handle-to-get-request-in-in-same-fbvfunction-based-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!