Django:视图调用另一个模板(分页)

时间:2018-07-31 20:30:01

标签: python django

我有这个观点:

class DamageListCriteria(TemplateView):
template_name = "damage/damagelist_criteria.html"

def get(self, request):
    form = DamageListCriteriaForm()
    general = General.objects.get(pk=1)
    args = {
        'form': form,
        'general': general
    }
    return render(request, self.template_name, args)

def post(self, request):
    general = General.objects.get(pk=1)
    form = DamageListCriteriaForm(request.POST)
    form.non_field_errors()

    if form.is_valid():
        fromdate = request.POST.get('fromdate')
        fdate = datetime.strptime(fromdate, '%d/%m/%Y')
        fdate = datetime.combine(fdate, datetime.min.time(), tzinfo=pytz.UTC)
        print('fdate ', fdate)
        todate = form.cleaned_data['todate']
        #tdate = datetime.strptime(todate, '%d/%m/%Y') + timedelta(days=1)
        tdate = datetime.strptime(todate, '%d/%m/%Y')
        tdate = datetime.combine(tdate, datetime.max.time(), tzinfo=pytz.UTC)
        print('tdate ', tdate)

        d_list = Damage.objects.filter(entry_date__range=(fdate, tdate))
        paginator = Paginator(d_list, 1)

        page = request.GET.get('page')
        try:
            damage_list = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            damage_list = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            damage_list = paginator.page(paginator.num_pages)

        template = "damage/damagelist_table.html"
        form = DamageListForm()
        general = General.objects.get(pk=1)

        fromdatetext = fdate.strftime('%d/%m/%Y')
        todatetext = tdate.strftime('%d/%m/%Y')

        args = {
            'form': form,
            'damage_list': damage_list,
            'general': general,
            'fromdate': fromdatetext,
            'todate': todatetext
        }
        return render(request, template, args)


    else:
        print('form is not valid')
        print(form.errors)
        # form = DamageEntryForm()
        args = {'form': form,
                'general': general
                }
        return render(request, self.template_name, args)

我想获取一些标准来列出我的数据库。 它以这种方式工作,直到我试图添加分页的那一刻。 网址为http://127.0.0.1:8000/damage/damage/list/criteria/

url(r'damage/list/criteria/$', views.DamageListCriteria.as_view(), name="damage-list-criteria"),

下一个和上一个无效,因为我在

之后仍在此网址上
return render(request, template, args)

我可以以某种方式重定向吗? 我了解这可能是错误的上市方式。你能帮我,怎么做?

非常感谢

Kostas

1 个答案:

答案 0 :(得分:-1)

最简单的方法不是使用Django本身,而是使用Django REST框架,并重用其序列化器类以及APIView(或其子类之一)。您能使用它还是受到限制?