django点击按钮返回同一页面

时间:2015-09-04 07:00:06

标签: python django

您好我有一个带有表单和按钮的html:

<form action="{% url 'packOrders' %}" method="POST">
    {% csrf_token %}
    <button name="pack_orders" id="pack_orders" class="btn btn-purple btn-labeled fa">Pack Orders</button>
    <table class="table table-striped">
        <thead>
            <tr>
                <th><input type="checkbox" onClick="toggle(this, 'no')"></th>
                <th>Invoice</th>
                <th>User</th>
                <th>Order date</th>
                <th>Amount</th>
                <th>Status</th>
                <th>Tracking Number</th>
            </tr>
        </thead>
        <tbody>
            {% for oid,dbd,stts,tp in new_orders %}
            <tr>
                <td><input type="checkbox" name="no" value="{{oid}}"></td>
                <td><a class="btn-link" href="#">{{oid}}</a></td>
                <td>Steve N. Horton</td>
                <td><span class="text-muted"><i class="fa fa-clock-o"></i>{{dbd}}</span></td>
                <td>{{tp}}</td>
                <td>{{stts}}</td>
                <td>-</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</form>

我的urls.py:

from django.conf.urls import patterns
from django.conf.urls import url
from orders import views

urlpatterns = patterns('',
                    url(r'^$', views.orders, name='orders'),
                    url(r'^test/', views.packOrders, name='packOrders'),)

Views.py:

from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from orders import forms


def orders(request):
    return render(request, 'orders/orders.html', context_dict)


def packOrders(request):
    oid_list = []
    form = forms.PackOrders(request.POST or None)
    if request.method == 'POST':
        print form.is_valid()
        list_of_ids = request.POST.getlist('no')
        for id in list_of_ids:
            oid_list.append(id)
    return HttpResponse(','.join(oid_list))

目前在orders.html页面中,当我选中复选框并按下按钮时,它会转到orders.html/test并从列表中打印该项目。我想点击按钮Pack Orders,它只是重新路由到orders.html或者用当前值刷新它。有一个后端脚本可以在项目列表中工作,所以我只需要在packOrders方法中调用一个方法。

注意:我没有在views.py中的orders方法中编写代码,该代码在orders.html中显示数据。

如何在urls.py中重新路由它?我尝试了几种解决方案,包括HttpResponseRedirect,可能但是没有用。

编辑:

项目urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', include('login.urls')),
    url(r'^login.html/', include('login.urls')),
    url(r'^index.html/', include('dashboard.urls')),
    url(r'^orders.html/', include('orders.urls')),
]

1 个答案:

答案 0 :(得分:3)

您可以使用:

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse


def packOrders(request):
    # your code
    return HttpResponseRedirect(reverse('orders'))

这将是最简单的解决方案。此外,您只需将代码“打包”到一个视图中,使用一些JavaScript或只在packOrders视图中再次渲染数据。

但推荐的方法是在post方法之后始终使用重定向 - 这样用户在刷新页面时将无法再次发送数据。

此外,您可以使用FormView:

from django.views.generic import FormView
from django.core.urlresolvers import reverse_lazy

class PackOrdersView(FormView):
    form_class = PackOrders

    success_url = reverse_lazy('orders') # we can't use normal reverse here, because it will be evaluated before urlconf is parsed

    def form_valid(self, form):

        # your code here

        return super(PackOrdersView, self).form_valid(form)

基于类的视图更灵活,可以为您完成大量工作,因此您不必自己处理表单,对象创建等。