2017-05-04 13 views
24

के बजाय एक नियम होना चाहिए, बस मेरे रूपों में से एक को सेंट्री त्रुटि 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', 
           'char[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

[* संदर्भ प्रदान की जाती है, तो यह करना होगा:

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

आप render_to_string शॉर्टकट का उपयोग करना चाहें एक निर्देश बनें। *] (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+ में एक त्रुटि देता है।

आपके मामले में, सिर्फ एक Context उदाहरण के बजाय एक नियमित रूप से dict का उपयोग करें:

from django.template.loader import render_to_string 

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

यह केवल तभी होता है जब आप टेम्पलेट्स में लोड हो रहे हों? यह मेरे लिए उत्सुक है क्योंकि खोल में मैंने एक टेस्ट टेम्पलेट बनाया था, फिर उसने केवल एक नियम पारित करने की कोशिश की लेकिन यह शिकायत कर रही थी कि यह एक संदर्भ नहीं था, जो मेरे लिए अजीब लगता है कि यह एक के लिए एक तरीका होगा लेकिन वैसे ही नहीं अन्य –

+2

@ डेविड टोर्रे के लिए यदि आप मैन्युअल रूप से 'django.template.Template' बनाते हैं तो आपको अभी भी' संदर्भ 'उदाहरण पास करना होगा। यह थोड़ा उलझन में है कि एपीआई अलग है कि आप 'टेम्पलेट = टेम्पलेट (...)' या 'टेम्पलेट = _template (...) प्राप्त करते हैं या नहीं। – Alasdair

संबंधित मुद्दे