Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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表单get请求公开数据_Python_Django_Django Forms_Django Class Based Views_Formview - Fatal编程技术网

Python 使用django表单get请求公开数据

Python 使用django表单get请求公开数据,python,django,django-forms,django-class-based-views,formview,Python,Django,Django Forms,Django Class Based Views,Formview,我正在构建一个小表单,在该表单上显示表中的一些数据,此外,我还有两个下拉列表,您可以使用它们为希望在表中看到的数据选择当前数据或年份 我的问题是如何使用django表单get请求填充下拉列表当前月份和年份,我不太清楚如何在我的视图中处理这个问题,请注意,我使用的是CBVFormView 我试过这样的东西 form.py 在我看来,我正在展示一张桌子,我需要做剩下的事情 view.py from django.views.generic.edit import FormView from .mo

我正在构建一个小表单,在该表单上显示表中的一些数据,此外,我还有两个
下拉列表
,您可以使用它们为希望在表中看到的数据选择当前数据或年份

我的问题是如何使用
django
表单get请求填充
下拉列表
当前月份和年份,我不太清楚如何在我的视图中处理这个问题,请注意,我使用的是CBV
FormView

我试过这样的东西 form.py

在我看来,我正在展示一张桌子,我需要做剩下的事情

view.py

from django.views.generic.edit import FormView

from .models import Rate
from statistics.forms import StatisticsForm
from statistics.services import StatisticsCalculation


class StatisticsView(FormView):
    """
    TODO: We need to handle
        Total Invoice - no matter how old, basically all of them
        Current month Total Invoice
    """
    template_name = "statistics/invoice_statistics.html"
    form_class = StatisticsForm

    def get_context_data(self, **kwargs):
        context = super(StatisticsView, self).get_context_data(**kwargs)

        def_currency = Rate.EUR

        context["can_view"] = self.request.user.is_superuser
        context["currency"] = def_currency
        context["supplier_statistic"] = StatisticsCalculation.statistic_calculation(default_currency)
        return context

FormView
创建实际的表单对象时,它从以下位置获取要传递到表单的参数:

注意它是如何在自身(视图)而不是表单上调用
get_initial()
。它无法在窗体上调用它,因为它尚未初始化。将您的方法移动到视图中,您就可以开始了

作为旁注,请使用
django.utils.timezone.now()
而不是stdlib
datetime.date.today()
,因为它尊重您的django时区设置,您可能偶尔会因为其他方面的怪癖而忽略一些设置

编辑:您还应该更新表单以使用
选择字段
,并使用
时区.now().month
时区.now().year设置默认值


愉快的编码。

我想在这个字段上使用
select2
库,这就是为什么我要创建
CharFields
,我想避免
ChoiceField
我是对的或者犯了错误,第一次使用
select2
。假设您使用的是
django-select2
,(然后遵循所有其他安装文档)。
from django.views.generic.edit import FormView

from .models import Rate
from statistics.forms import StatisticsForm
from statistics.services import StatisticsCalculation


class StatisticsView(FormView):
    """
    TODO: We need to handle
        Total Invoice - no matter how old, basically all of them
        Current month Total Invoice
    """
    template_name = "statistics/invoice_statistics.html"
    form_class = StatisticsForm

    def get_context_data(self, **kwargs):
        context = super(StatisticsView, self).get_context_data(**kwargs)

        def_currency = Rate.EUR

        context["can_view"] = self.request.user.is_superuser
        context["currency"] = def_currency
        context["supplier_statistic"] = StatisticsCalculation.statistic_calculation(default_currency)
        return context
def get_form_kwargs(self):
    """
    Returns the keyword arguments for instantiating the form.
    """
    kwargs = {
        'initial': self.get_initial(),
        'prefix': self.get_prefix(),
    }
    if self.request.method in ('POST', 'PUT'):
        kwargs.update({
            'data': self.request.POST,
            'files': self.request.FILES,
        })
 return kwargs