python-Django 1.11 TypeError上下文必须是字典而不是Contex

刚刚在我的一种表格上收到Sentry错误message = get_template('email_forms/direct_donation_form_email.html').render(Context(ctx))。 我知道它与Django 1.11有关,但是我不确定要进行哪些更改以修复它。

违规线

message = get_template('email_forms/direct_donation_form_email.html').render(Context(ctx))

整个视图

def donation_application(request):
    if request.method == 'POST':
        form = DirectDonationForm(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', '')
            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 Donation'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = ['deniselarkins@/////\\\\\.com',
                              'charles@/////\\\\\.net',
                              'marcmunic@/////\\\\\.com',
                              ]

            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,
            )

            total_support = decimal.Decimal(support) / 100
            total_charge = decimal.Decimal(int(support)) / 100

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

            ctx = {
                'name': name,
                'address': address,
                'city': city,
                'state': state,
                'zip': zip,
                'phone_number': phone_number,
                'email_address': email_address,
                'agree': agree,
                'charge': charge,
                'customer': customer,
                'total_support': total_support,
                'total_charge': total_charge
            }

            message = get_template('email_forms/direct_donation_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/donation-support-thank-you/?name=' + name +
                '&address=' + address +
                '&state=' + state +
                '&city=' + city +
                '&zip=' + zip +
                '&phone_number=' + phone_number +
                '&email_address=' + email_address +
                '&total_support=' + str(total_support) +
                '&total_charge=' + str(total_charge)
            )
    context = {
        'title': 'Donation Pledge',
    }

    return render(request, 'contribute/_donation-application.html', context)
Studio Rooster asked 2020-08-10T14:29:07Z
3个解决方案
55 votes

在Django 1.8+中,模板的render_to_string方法采用了Context参数的字典。 不赞成支持传递Context实例,并且在Django 1.10+中给出了错误。

在您的情况下,只需使用常规render_to_string而不是Context实例:

message = get_template('email_forms/direct_donation_form_email.html').render(ctx)

您可能更喜欢使用render_to_string快捷方式:

from django.template.loader import render_to_string

message = render_to_string('email_forms/direct_donation_form_email.html', ctx)

如果您使用的是RequestContext而不是Context,则也应将request传递给这些方法,以便上下文处理器运行。

message = get_template('email_forms/direct_donation_form_email.html').render(ctx, request=request)
message = render_to_string('email_forms/direct_donation_form_email.html', ctx, request=request)
Alasdair answered 2020-08-10T14:29:27Z
9 votes

从Django 1.8迁移到Django 1.11.6

无论我在哪里有RequestContext类,都有一种方法flatten()可以将结果作为字典返回。

因此,如果该类是RequestContext。

return t.render(context)

变成

return t.render(context.flatten())

在上下文被Context()包装的情况下,只需将其删除即可。 因为不推荐使用Context()。

return t.render(Context(ctx))

变成

return t.render(ctx)
VIctor Angelov answered 2020-08-10T14:30:10Z
0 votes

对于Django 1.11及更高版本,上下文必须是dict。

您可以使用:

context_dict = get_context_dict(context)
return t.render(context_dict)

要么

context_dict = context.flatten()
return t.render(context_dict)
Yvette Tan answered 2020-08-10T14:30:39Z
translate from https://stackoverflow.com:/questions/43787700/django-1-11-typeerror-context-must-be-a-dict-rather-than-context