同一页面上有两个动作

时间:2017-08-23 13:22:41

标签: django

目前,我在两个页面上有两个功能:

  • 列出所有订单(/订单)
  • 创建新订单(/ create)

我希望能够在与列表相同的页面上创建订单。

在我的urls.py中:

url(r'^create/$', views.OrderCreateView.as_view(),name='create'),

在我的forms.py中:

class OrderCreateForm(forms.ModelForm):
class Meta:
    model = models.TransDetails
    fields = ("__all__")

在我的views.py中:

class PlaceListView(LoginRequiredMixin,ListView):
  login_url = '/login/'
  context_object_name='placeList'
  model= models.Places
  template_name = 'transadmin/place_list.html'

  def get_queryset(self):
    return models.Places.objects.all().order_by('pname')

class PlaceCreateView(LoginRequiredMixin,CreateView):
  login_url = '/login/'
  form_class = PlaceCreateForm
  success_url = reverse_lazy("transadmin:place_list")
  template_name = 'transadmin/create_place.html'

我已经尝试在列表页面上添加一个表单来添加但是当提交时没有发生任何事情。我该怎么做?

编辑:

模板:

    {% extends "transadmin/base.html" %} {% block body_block %} {% if user.is_superuser %}
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
    <div class="row x_title">
        <div class="col-md-6">
            <h3>Orders</h3>
        </div>
    </div>
    <table id="orderTable" class="table" data-editing="true" data-paging-size="5" data-paging="true" data-filtering="true" data-sorting="true" data-state="false">
        <thead>
            <tr>
                <th>ID</th>
                <th data-breakpoints="xs sm">Origin</th>
                <th data-breakpoints="xs sm">Destination</th>
                <th data-breakpoints="xs sm md">Mode</th>
                <th>Status</th>

            </tr>
        </thead>
        <tbody>
            {% for order in orderList %}
            <tr>
                <td>{{order.id}}</td>
                <td>{{order.origin}} </td>
                <td>{{order.mode}} </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
    <div class="x_title">
        <h3>Create an order</small></h3>
        <div class="clearfix"></div>
    </div>
    <div class="x_content">
        <br />

        <div class="form-group">
            <form method="POST" class="form-horizontal form-label-left">
                {% csrf_token %} {{ form|bootstrap_horizontal }}
                <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-6">
                        <input type="submit" class="btn btn-fill btn-success pull-right" value="Submit">
                    </div>
                </div>

            </form>

        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

您需要在表单声明中指定action参数:

<form method="POST" action="{% url 'transadmin:create' %}"
      class="form-horizontal form-label-left">
    {% csrf_token %} {{ form|bootstrap_horizontal }}
    <div class="form-group">
        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-6">
            <input type="submit" class="btn btn-fill btn-success pull-right" value="Submit">
        </div>
    </div>
</form>