2017-02-09 10 views
1

現在、私は現在、ページの更新時に繰り返し送信されるのを避けるために、フォームの送信後にHttpResponseRedirectを実行する必要があるという事実を回避しようとしています。フォームアクションはまったく起動していません

フォームの[送信]ボタンをクリックすると、別のビューがトリガされ、現在のビューは送信されません。ここに私のHTMLは次のとおりです。 (水平テーブルをレンダリングするためのコードの多くがありますので、私はフォームを撮影した)

<form id= "time-form" method = 'POST' action='' class="dynamic-form" enctype="multipart/form-data">{% csrf_token %} 
      <div id="formset-container" class = "formset-container"> 
       <table> 
       <--form is in here--> 
       </table> 
       <ul>{{ newtime_formset.errors }}</ul> 
      </div> 
      </br> 
      <div> 
       <input type = "submit" id = "save" action="{% url 'tande:create_time' %}" value = "Save Timesheet"> 
      </div> 

だから私はCREATE_TIMEビューを実行するための送信ボタンをしたいです。私たちは現在、タイムシートと呼ばれるビューにいます。しかし、それは現在のビューに留まるだけで、create_timeビューには入っていません。

def create_time(request): 
    #below isn't printing 
    print "create_time view" 
    #save the form in here 
    return HttpResponseRedirect('timesheet') 

、両方のビューのURL:

from django.conf.urls import url 

from . import views 

urlpatterns = [ 
    url(r'^(?P<timesheet_id>[0-9]+)/person/timesheet/$', views.timesheet, name='timesheet'), 
    url(r'^create_time/$', views.create_time, name='create_time'), 
] 

だから、私はそれが理にかなっていると思います.... CREATE_TIMEにタイムシートを保存し、リダイレクトを回避するために戻ってタイムシートに行きたいです.. ..:

ありがとう

答えて

2

フォームのアクションパラメータは、ページがどこにボタンを送信しないかを定義します。

<form id= "time-form" method = 'POST' action="{% url 'tande:create_time' %}" class="dynamic-form" enctype="multipart/form-data">{% csrf_token %} 
    <div id="formset-container" class = "formset-container"> 
      <table> 
      <--form is in here--> 
      </table> 
      <ul>{{ newtime_formset.errors }}</ul> 
     </div> 
     </br> 
     <div> 
      <input type = "submit" id = "save" value = "Save Timesheet"> 
    </div> 
</form> 

はまた、あなたのビューで、あなたは逆のURLリゾルバに

def create_time(request): 
    #below isn't printing 
    print "create_time view" 
    #save the form in here 
    return HttpResponseRedirect(reverse('timesheet', kwargs={"timesheet_id":<timesheet_id>})) 
+0

もう一つの問題はジャンゴする必要があります、私はtimesheet_idグローバル名が定義されていない取得します。だから私はid値である 'kwargs = {" timesheet_id ":timesheet_object}'を試してNoReverseMatchを得ました.... **引数 '()'とキーワード引数 'timesheet_id': 'timesheet' '34'} 'が見つかりませんでした。 0個のパターンが試されました:[] ** –

+0

これは規約であり、タイムシートIDを含む変数で置き換える必要があります。あなたが作成したオブジェクトのIDは、一般的には –

+0

の投稿フォームに投稿してください!上記の '34'は私のタイムシートIDです...私はURLにそれが必要です.. –

関連する問題