Django表单提交重定向问题

时间:2018-12-11 15:59:31

标签: html django django-forms django-views

我已经创建了一个客户模型和一个商店模型。当我通过表单添加/编辑商店时,其重定向到页面的空列表而不是商店列表。我将不得不刷新页面才能看到内容。 这是我的代码,有人可以帮助我确定错误并纠正吗?

观看次数:

class CustomerDetailView(DetailView):
    context_object_name = 'customerdetail_view'
    queryset = Customer.objects.order_by('id')
    model = Customer
    paginate_by = 10
    template_name = 'App_CUS/customer_detail.html'


def save_store_formview(request, form, template_name):
    data = dict()
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            data['form_is_valid'] = True
            stores = Store.objects.all()
            data['html_store_list'] = render_to_string('App_CUS/stores/partial_store_list.html', {
                'stores': stores
            })
        else:
            data['form_is_valid'] = False
    context = {'form': form}
    data['html_form'] = render_to_string(template_name, context, request=request)
    return JsonResponse(data)


def store_createview(request):
    if request.method == 'POST':
        form = StoreForm(request.POST)
    else:
        form = StoreForm()
    return save_store_formview(request, form, 'App_CUS/stores/partial_store_create.html')


def store_updateview(request, pk):
    store = get_object_or_404(Store, pk=pk)
    if request.method == 'POST':
        form = StoreForm(request.POST, instance=store)
    else:
        form = StoreForm(instance=store)
    return save_store_formview(request, form, 'App_CUS/stores/partial_store_update.html')  

def store_deleteview(request, pk):
    store = get_object_or_404(Store, pk=pk)
    data = dict()
    if request.method == 'POST':
        store.delete()
        data['form_is_valid'] = True
        stores = Store.objects.all()
        data['html_store_list'] = render_to_string('App_CUS/stores/partial_store_list.html', {
            'stores': stores
        })
    else:
        context = {'store': store}
        data['html_form'] = render_to_string('App_CUS/stores/partial_store_delete.html', context, request=request)
    return JsonResponse(data)
形式:
class StoreForm(forms.ModelForm):
  class Meta:
    model = Store
    fields = '__all__'
的HTML:
    {% for store in customerdetail_view.stores.all %}
      <tr class="item">
        <td>{{store.store}}</td>
        <td>{{ store.customer }}</td>
        <td>{{ store.status }}</td>
        <td>{{ store.description }}</td>
        <td style="width: 150px">
          <button type="button"
                  class="btn btn-outline-warning btn-sm js-update-store" style="border:none;"
                  data-url="{% url 'App_CUS:store_update' store.id %}">
            <span class="glyphicon glyphicon-pencil"></span> Edit
          </button>
          <button type="button"
                  class="btn btn-outline-danger btn-sm js-delete-store" style="border:none;"
                  data-url="{% url 'App_CUS:store_delete' store.id %}">
            <span class="glyphicon glyphicon-trash"></span> Delete
          </button>
        </td>
      </tr>
    {% empty %}
      <tr>
        <td colspan="8" class="text-center bg-warning">No Stores</td>
      </tr>
    {% endfor %}
Ajax代码:
$(function () {

  /* Functions */

  var loadForm = function () {
    var btn = $(this);
    $.ajax({
      url: btn.attr("data-url"),
      type: 'get',
      dataType: 'json',
      beforeSend: function () {
        $("#modal-store .modal-content").html("");
        $("#modal-store").modal("show");
      },
      success: function (data) {
        $("#modal-store .modal-content").html(data.html_form);
      }
    });
  };

  var saveForm = function () {
    var form = $(this);
    $.ajax({
      url: form.attr("action"),
      data: form.serialize(),
      type: form.attr("method"),
      dataType: 'json',
      success: function (data) {
        if (data.form_is_valid) {
          $("#store-table tbody").html(data.html_store_list);
          $("#modal-store").modal("hide");
        }
        else {
          $("#modal-store .modal-content").html(data.html_form);
        }
      }
    });
    return false;
  };


  /* Binding */

  // Create store
  $(".js-create-store").click(loadForm);
  $("#modal-store").on("submit", ".js-store-create-form", saveForm);

  // Update store
  $("#store-table").on("click", ".js-update-store", loadForm);
  $("#modal-store").on("submit", ".js-store-update-form", saveForm);

  // Delete store
  $("#store-table").on("click", ".js-delete-store", loadForm);
  $("#modal-store").on("submit", ".js-store-delete-form", saveForm);

});
我假设函数“ save_store_formview”下的错误类似于“ stores = Store.objects.all()”。既然如此,这是否无法将我们带到特定客户的商店列表?它会显示一条警告消息“没有存储”,如html中所指定。提交表单(添加/编辑商店)而不是达到“无商店”条件后,如何使它返回到特定客户的数据(商店)列表?

我正在学习Django,并遵循了Vitor的simpleisbetterthancomplex教程。谢谢!

1 个答案:

答案 0 :(得分:0)

也许这只是发布时的错误,但您缺少def save_store_formview(request, form, template_name):下的语句的缩进

这意味着您的功能没有主体-会引起很多问题。