表格行动根本没有触发

时间:2017-02-09 14:21:53

标签: python html django forms

基本上我目前正在努力解决这样一个事实:你必须在提交表单后做一个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')

两个视图的网址:

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中保存时间表,然后返回时间表以避免重定向....我认为这是有道理的......:S

由于

1 个答案:

答案 0 :(得分: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>

同样在您的视图中,您将需要django反向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>}))