Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django中基于类的视图中的转换函数视图_Python_Django_Class - Fatal编程技术网

Python Django中基于类的视图中的转换函数视图

Python Django中基于类的视图中的转换函数视图,python,django,class,Python,Django,Class,我有一个带有表单的存档页面,有两个选择选项输入,分别是年和月,我想从数据库中选择在该页面中选择的年和月创建的对象,我有这个功能视图,工作正常,但我需要CBV版本,我尝试了view,但不起作用,请提供一些帮助 def archive(request): if request.method == 'POST': year = datetime.strptime(request.POST.get('year'), '%Y') month = datetime.s

我有一个带有表单的存档页面,有两个选择选项输入,分别是年和月,我想从数据库中选择在该页面中选择的年和月创建的对象,我有这个功能视图,工作正常,但我需要CBV版本,我尝试了view,但不起作用,请提供一些帮助

def archive(request):
    if request.method == 'POST':
        year = datetime.strptime(request.POST.get('year'), '%Y')
        month = datetime.strptime(request.POST.get('month'), '%m')
        incomes = Income.objects.filter(
            user=request.user, created_date__year=year.year, created_date__month=month.month)
        spendings = Spending.objects.filter(
            user=request.user, created_date__year=year.year, created_date__month=month.month)
    else:
        incomes = Income.objects.filter(user=request.user)
        spendings = Spending.objects.filter(user=request.user)
        year, month = False, False

    context = {
        'title': 'Archive',
        'spendings': spendings,
        'incomes': incomes,
        'currency': Profile.objects.get(user=request.user).currency,
        'total_incomes': round(assembly(incomes), 2),
        'total_spendings': round(assembly(spendings), 2),
        'total_savings': round(assembly(incomes) - assembly(spendings), 2),
        'year': year,
        'month': month,
    }
    return render(request, 'users/archive.html', context)

尝试编写一个类,如下所示:-

class MyView(View):
    template_name = 'mytemplate.html'
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        # change context in case of get method / other code
        return render(request, self.template_name, context)
    def post(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        # change context in case of post method / other code
        return render(request, self.template_name, context)
    def get_context_data(self, **kwargs):
        context = {}
        # common code to run in case of any method
        return context

你能详细说明一下你需要这个CBV是什么样子和做什么吗?我需要一个CVB来呈现页面的第一次访问的地方将列出登录用户的所有对象,在这种情况下,对象是在else语句中获得的,页面上是一个带有2个select类型输入的表单。用户将选择一年和一个月,在他们点击submit之后,if语句将从请求中获得年和月,它将根据这些参数过滤对象,并只给它们这些对象谢谢,它工作得非常好:D