2017-02-16 8 views
0

データを表示するフォームと、データを表示する日付/年を選択できる2つの選択ドロップダウンウィートがありますので、contextとテーブルを返します。get_initialです現在の日付と時刻を使用するようにドロップダウンを設定します。djangoは自身のページにリダイレクトします

私の質問はPOST要求がGETでそれ自身のページにリダイレクトするようにする方法、アイデアは?month=1&year=2016のようなURLにいくつかのクエリパラメータの値を設定することですが、私はどこから始めればわからないが、誰かが私を助けることができますこれを理解して、ありがとう。フォームの

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 

答えて

0

記事では、HTMLフォームでそれらを設定する場所に掲載します。

このようにフォーム要素を設定すると、urls.pyが正しく設定されていることを前提として、取得と投稿の両方が表示されます。

<form method='post' action =''> 

あなたurl_patternsのような何かの行を含める必要がありますので、 `url`がここにトリックです

url(r'the-url-you want-to-use', StatisticsView.as_view(), name='name_for_reverse_function') 
+0

が、それは簡単な' url'ですので、あなたが私の 'のURLを設定する方法を私に説明することができます'、私の質問を更新しましょう。 – PetarP

+0

正しいと思われる、あなたのURLはどのようなビューに向けられていますか? –

+0

ちょうど私が得ていることは、データをブラウズしたいときにフォームがリダイレクトされていることです。私は彼がそのページにとどまり、さらにデータを表示したいと思っています。私の質問では、私は月と年の2つのフィールドを持つ単純なフォームを持っています – PetarP

関連する問題