2017-05-04 60 views
24

剛剛在我的某個表單上收到了Sentry錯誤TypeError context must be a dict rather than Context.。我知道這與Django 1.11有關,但我不確定要修改它。Django 1.11 TypeError上下文必須是字典而不是上下文

問題的行

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 = ['[email protected]/////\\\\\.com', 
           '[email protected]/////\\\\\.net', 
           '[email protected]/////\\\\\.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) 
+1

[*如果提供方面,必須是一個字典。*](https://docs.djangoproject.com/en/1.11/topics/templates/#django.template.backends.base.Template.render) –

回答

35

Django 1.8+,模板的render方法需要一本字典爲context參數。支持傳遞Context實例is deprecated,並在Django 1.10+中給出錯誤。

在你的情況下,只需使用常規dict代替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) 
+0

僅當您在模板中加載時才適用?它對我很好奇,因爲在shell中我創建了一個測試模板,然後嘗試傳遞一個字典,但它抱怨說它不是一個上下文,對我來說這似乎很奇怪,它將是一種方式但不是相同的方式對於其他 –

+2

@DavidTorrey如果您手動構建'django.template.Template',那麼您仍然必須傳遞一個'Context'實例。根據是否執行'template = Template(...)'或'template = get _template(...)''''',API有點混亂。 – Alasdair

相關問題