【问题标题】:Is there a method to post form data from one url to another url, and then render the template based on the form data有没有办法将表单数据从一个url发布到另一个url,然后根据表单数据渲染模板
【发布时间】:2019-08-11 20:29:21
【问题描述】:

我正在尝试创建一个简单的单品产品商店,客户可以在其中进入产品页面并在表格中选择他们想要购买的数量。完成表格后,我希望它重定向到结帐页面并呈现他们选择的数量。有没有一种简单的方法可以做到这一点?目前,我将表单数据发布到产品页面 url,然后将用户重定向到结帐页面,但是我不确定如何访问该数据。

def proxy_detail(request, proxy_slug):
    if request.method == 'POST':
        form = forms.AddProxyAmountForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            number_of_proxies = int(cd['number_of_proxies'])
            return redirect('payment:checkout')
    else:
        add_proxy_form = forms.AddProxyAmountForm()
        proxy_product = get_object_or_404(models.ProxyProduct, slug = proxy_slug)
        return render(request, 'shop/product/proxy_detail.html', {'proxy_product' : proxy_product, 'add_proxy_form' : add_proxy_form })

【问题讨论】:

  • 您能告诉我们您目前尝试了什么吗?谢谢!
  • 这是我目前正在做的事情,我将表单发布到产品页面,然后重定向到结帐页面,但是我需要访问结帐页面上的变量 number_of_proxies

标签: django python-3.x


【解决方案1】:

这不是一个确切的答案,但我是新来的,所以我不能真正发表评论:p

无论如何,我认为在您看来,您可以做的是,获取变量中的数据并将其作为上下文传递以呈现另一个页面。

def viewname(request):
    if request.method == "POST":
        #your logic to save here
        context = {variableOfInterest : request.POST.valueOfInterest} #considering you are getting it from the form
        return render(request, "redirected.html", context)

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 2017-12-17
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2016-01-31
    • 2016-05-17
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多