0
我有一個表格顯示數據和兩個選擇下拉菜單誰可以選擇日期/年顯示數據,所以我返回context
表和get_initial
我是填充下拉菜單以使用當前日期和時間。django發佈重定向到它自己的頁面
我的問題是,如何讓POST
請求重定向到與GET
它自己的頁面,這個想法是在像?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
所以'url'是這裏的竅門,這是一個簡單的'url',這樣你就可以解釋我如何設置我的'網址',讓我更新我的問題。 – PetarP
它看起來是正確的,你的網站正在引導什麼觀點? –
正是我得到的,事情是,當我想要瀏覽數據時窗體正在重定向,而我不想那樣,我希望他留在那個頁面上並向我展示更多數據 - 你是什麼意思,使用'FormView',這是在我的問題,我有一個簡單的形式與兩個字段的月和年 – PetarP