Django pass object from view to next for processing

后端 未结 2 1517
小鲜肉
小鲜肉 2021-02-08 23:02

If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to crea

2条回答
  •  太阳男子
    2021-02-08 23:48

    Use a HttpResponseRedirect to direct to the table view w/the newly created object's id. Here's an abbreviated example:

    def first(request):
        if request.method == 'POST':
              form = MyModelForm(request.POST, request.FILES)
              if form.is_valid():
                   my_model = form.save()
    
                   return HttpResponseRedirect('/second/%s/' % (my_model.pk)) # should actually use reverse here.
          # normal get stuff here
    
    def second(request, my_model_pk):
         my_model = MyModel.objects.get(pk=my_model_pk)
    
         # return your template w/my model in the context and render
    

提交回复
热议问题