【问题标题】:Redirecting from one form to another form in Django在 Django 中从一个表单重定向到另一个表单
【发布时间】:2015-06-09 00:15:28
【问题描述】:

我有两个表单页面:

  1. 开始表格 - 输入基本信息的地方
  2. 添加产品表单 - 使用开始表单中的数据填充某些字段。您还可以在表格中提供更多信息。

在 urls.py 我有这个:

urlpatterns = patterns('',
    url(r'^$',add_product_start),
    url(r'^add_product/$', add_product),
    )

这是我的 add_product_start 表单视图:

def add_product_start(request):
    form = add_product_start_form()
    if request.method == "POST":
        form = add_product_start_form(request.POST)
        if form.is_valid:
            #return respose to the add_product url with request data
    return render(request,'index.html',{'form':form})

这是我的 add_product 视图:

def add_product(request):
    images = []
    if request.method == "POST":
        initial = {field:value for (field,value) in request._post.iteritems() if value}
        #a bunch of other code
        #point is I want to receive post data from add_product_start view and at the same time redirect to a different url, because I don't want to have the same url of two different forms.
    return render(request,'add_product.html' ,{'form':form,'images':images})

我知道有类似 HttpResponseRedirect 之类的东西,但它们只是将我重定向到没有任何类型数据的页面。我想从开始表单中获取数据,对其进行验证,如果有效,将其传递给具有不同类型的第二个视图页面。

谁能帮帮我。非常感谢!

【问题讨论】:

    标签: python django forms request


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    我使用redirect(obj) 解决了类似的问题。 该视频可能会有所帮助 https://www.youtube.com/watch?v=YsHd-l7QdI8

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2016-02-28
    • 1970-01-01
    • 2011-08-04
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2015-11-10
    • 2015-02-08
    相关资源
    最近更新 更多