简体   繁体   English

Django:如何在表单 post bulk_create 后重定向到包含新创建对象列表的页面

[英]Django: how to redirect to page with list of newly created objects after form post bulk_create

I have a simple form that allows user to paste csv data with the goal to create multiple model objects at one go.我有一个简单的表单,允许用户粘贴 csv 数据,目的是在一个 go 上创建多个 model 对象。

The form method is 'post'.表单方法是'post'。 Action url is a method that calls the django `bulk_create` to create the model objects returning the pks.操作 url 是一种调用 django `bulk_create` 以创建 model 对象返回 pks 的方法。

Goal: After submission, I want to redirect to page showing a list of the objects corresponding to the pks.目标:提交后,我想重定向到显示与 pks 对应的对象列表的页面。

Is that possible?那可能吗?

I have tried:我努力了:

  1. return render(request, "list_template.html", context={'object_list': objects_with_pks}) , passing the model objects as context data to another template (but there would be danger of duplicate submission upon refresh) return render(request, "list_template.html", context={'object_list': objects_with_pks}) ,将 model 对象作为上下文数据传递给另一个模板(但刷新时会有重复提交的危险)
  2. Redirect user to a model DetailView which contains the pk in the url (but I have more than one pks created)将用户重定向到 model DetailView ,其中包含 url 中的 pk(但我创建了多个 pk)
  3. return HttpResponseRedirect to a new page with a success message (but I won't be able to pass the pks to the new page)HttpResponseRedirect返回到带有成功消息的新页面(但我将无法将 pks 传递到新页面)
  4. Pass num = len(pks) to a new view function that accepts a num kwargs in its url and returns the last num model objects (but is there a proper way?)num = len(pks)传递给新视图 function ,该视图在其 url 中接受num kwargs 并返回最后一个num Z20F35E630DAF44DBFA4C3F68F5399D8 对象(正确的方式是什么?)

Other ways that I've thought but could not find an answer我想过但找不到答案的其他方式

  1. Can I pass multiple pks in a url?我可以在 url 中传递多个 pk 吗? so ListView could set the queryset所以 ListView 可以设置查询集
  2. If not, can querysets in a model ListView instance be passed as parameters?如果不是,可以将 model ListView实例中的查询集作为参数传递吗?
  3. Is there a way to transform a POST request to a GET?有没有办法将 POST 请求转换为 GET?

I am fairly new to web development and posting questions on stackoverflow.我对 web 开发和在 stackoverflow 上发布问题相当陌生。 Please comment if its unclear and I'll update.如果不清楚,请发表评论,我会更新。 Thank you so much.太感谢了。

@jjxxll- Why would you want to pass multiple pk's to a URL? @jjxxll- 为什么要将多个 pk 传递给 URL? If you want to view all the inserted objects after the bulk_insert is completed then a simple ListView will do.如果您想在 bulk_insert 完成后查看所有插入的对象,那么一个简单的 ListView 就可以了。 Eg例如

 class ModelListView(ListView):
        model = Model
        template_name = 'some_form.html"
        paginate_by = 10
 In your some_form.html you will have to loop through the object_list and display the fields e.g 

{% for obj in object_list%}
                        {{obj.name}}
                       {% endfor %}

That's it而已

If you want to view DetailView per object using pk如果您想使用 pk 查看每个 object 的 DetailView

 class ModelDetailView(DetailView):
        model = Model
        template_name = 'some_detail_form.html'
   In your detail_form.html you will have to display the data e.g
        {{object.name}}
        {{object.description}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM