【发布时间】:2017-02-16 20:41:49
【问题描述】:
我有一个表格,它显示带有数据的表格和两个选择下拉菜单,您可以选择日期/年份来显示数据,所以我返回 context 用于表格和 get_initial 我正在填充下拉列表使用当前日期和时间。
我的问题是如何使POST 请求使用GET 重定向到它自己的页面,这个想法是在URL 中的一些查询参数中设置值,例如?month=1&year=2016,但我不知道从哪里开始,谁能帮我理解一下,谢谢。
from django.views.generic.edit import FormView
from django.utils import timezone
from .models import Rate
from statistics.forms import StatisticsForm
from statistics.services import StatisticsCalculation
class StatisticsView(FormView):
template_name = "statistics/invoice_statistics.html"
form_class = StatisticsForm
def get_initial(self):
initial = super(StatisticsView, self).get_initial()
initial["month_choice"] = timezone.now().month
initial["invoice_year"] = timezone.now().year
return initial
def get_context_data(self, **kwargs):
context = super(StatisticsView, self).get_context_data(**kwargs)
default_currency = Rate.EUR
currency_usd = Rate.USD
currency_gbp = Rate.GBP
context["can_view"] = self.request.user.is_superuser
context["currency"] = default_currency
context["currency_usd"] = currency_usd
context["currency_gbr"] = currency_gbp
context["statistic"] = StatisticsCalculation.\
statistic_calculation(supplier_default_currency)
context["statistic_usd"] = StatisticsCalculation. \
calculation(supplier_default_currency_usd)
context["statistic_gbp"] = StatisticsCalculation. \
statistic_calculation(supplier_default_currency_gbp)
return context
url 表格:
url(r'^statistic/$', login_required(views.StatisticsView.as_view()), name='statistics')
模板:
<form action="" method="post">{% csrf_token %}
<div class="" id="">
<label for="{{ form.month.month_choice }}"></label>
{{ form.month_choice }}
</div>
<br>
<div class="" id="">
<label for="{{ form.year.invoice_year }}"></label>
{{ form.invoice_year }}
</div>
<br>
# rest of the html
【问题讨论】:
标签: django django-forms django-class-based-views