将上下文从一个Django视图传递到另一个

时间:2017-02-04 08:37:38

标签: python django django-forms django-templates django-views

我有一个带有会员表格的Django视图,可以重定向到感谢页面。该表单与Stripe集成以进行付款。表格按预期工作;通过Stripe充电并重定向到感谢页面。我想传递上下文变量以显示在感谢页面上。我读过Django Sessions可以做到这一点,但我无法弄明白。以下是我的申请,谢谢你的页面浏览量。谢谢你的帮助。

def membership_application(request):
    # TODO - add method to check if id exists before posting
    # TODO - setup proper form validation
    # TODO - Setup billing
    if request.method == 'POST':
        form = MembershipForm(data=request.POST)
        if form.is_valid():
            stripe.api_key = settings.STRIPE_SECRET_KEY
            name = request.POST.get('name', '')
            address = request.POST.get('address', '')
            city = request.POST.get('city', '')
            state = request.POST.get('state', '')
            zip = request.POST.get('zip', '')
            phone_number = request.POST.get('phone_number', '')
            membership = request.POST.get('membership', '')
            support = request.POST.get('support', '')
            agree = request.POST.get('agree', '')
            email_address = request.POST.get('email_address', '')
            number = request.POST.get('number', '')
            cvc = request.POST.get('cvc', '')
            exp = request.POST.get('exp', '')
            # token = form.cleaned_data['stripe_token'],
            # exp_m = int(request.POST.get('exp_month', ''))
            # exp_y = int(request.POST.get('exp_year', ''))

            exp_month = exp[0:2]
            exp_year = exp[5:9]

            subject = 'New Plaque Order'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = [from_email]

            token = stripe.Token.create(
                card={
                    'number': number,
                    'exp_month': exp_month,
                    'exp_year': exp_year,
                    'cvc': cvc
                },
            )

            customer = stripe.Customer.create(
                email=email_address,
                source=token,
            )

            membership = stripe.Subscription.create(
                customer=customer.id,
                plan=membership,
            )

            total_charge = int(membership.plan.amount) + int(support)

            # Charge the user's card:
            charge = stripe.Charge.create(
                amount=total_charge,
                currency='usd',
                description='Soledad Memorial Membership Signup',
                customer=customer.id
            )

            # f_charge = Money(charge.amount, 'USD')  # Money(retrieved_charge.charge.amount, 'USD')

            ctx = {
                'title': 'Membership Application',
                'name': name,
                'address': address,
                'city': city,
                'state': state,
                'zip': zip,
                'phone_number': phone_number,
                'email_address': email_address,
                'number': number,
                'cvc': cvc,
                'charge': charge,
                'customer': customer,
                'membership': membership,
                'support': support,
                'agree': agree,
            }

            message = get_template('email_forms/membership_form_email.html').render(Context(ctx))
            msg = EmailMessage(subject, message, from_email=from_email, to=recipient_list)
            msg.content_subtype = 'html'
            msg.send(fail_silently=True)

            return redirect('/contribute/thank-you/', ctx)

    return render(request, 'contribute/_membership-application.html')


def membership_thank_you(request):
    ctx = {
        'title': 'Thank You'
    }
    return render(request, 'contribute/_membership-thank-you.html', ctx)

1 个答案:

答案 0 :(得分:1)

使用GET方法传递变量:

第一个功能

return redirect('/contribute/thank-you/?name=' + name + '&address=' + address)

第二功能

def membership_thank_you(request):
ctx = {
    'name': request.GET.get('name'),
    'address': request.GET.get('address')
}
return render(request, 'contribute/_membership-thank-you.html', ctx)