2017-03-01 42 views
-1

我有一個通訊註冊表單,它將數據發送到AgileCRM.com並創建一個新聯繫人。該表格部分有效;它會在Agile中創建新聯繫人,但不會發送EmailMessage通知,並且在控制檯中出現TypeError: the JSON object must be str, not 'bytes'錯誤。在這個主題上有超過幾個SO帖子,但我不知道如何將修復應用到我的情況。Django Ajax表單錯誤 - TypeError:JSON對象必須是str,而不是'bytes'

阿賈克斯

let fields = { 
      first_name: 'null', 
      last_name: 'null', 
      phone_number: 'null', 
      email_address: 'null', 
     }; 

     // Footer Form 
     $('#footerFormBTN').click(function (e) { 
      console.log("create post is working!"); 
      e.preventDefault(); 
      fields.first_name = $('#id_first_name').val(); 
      fields.last_name = $('#id_last_name').val(); 
      fields.phone_number = $('#id_phone_number').val(); 
      fields.email_address = $('#id_email_address').val(); 
      $('#loader').show(); 
      $.ajax({ 
       type: "POST", 
       url: $('#footerForm').attr('action'), 
       data: { 
        csrfmiddlewaretoken: getCookie('csrftoken'), 
        first_name: fields.first_name, 
        last_name: fields.last_name, 
        phone_number: fields.phone_number, 
        email_address: fields.email_address, 
       }, 
       cache: false, 
       success: function (data) { 
        $('#id_first_name, #id_last_name, #id_phone_number, #id_email_address').val(''); 
        $('#loader').hide(); 
        $("#order_message").html('<div class="alert alert-success"><button type="button" class="close">×</button>Thank you for joining.</div>'); 
        window.setTimeout(function() { 
         $(".alert").fadeTo(500, 0).slideUp(500, function() { 
          $(this).remove(); 
         }); 
        }, 3000); 
        console.log(data) 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        $('#id_first_name, #id_last_name, #id_phone_number, #id_email_address').val(''); 
        $('#loader').hide(); 
        console.log(jqXHR); 
        console.log(errorThrown); 
       } 
      }); 
      e.preventDefault(); 
     }); 

查看

def footer_form(request): 
    if request.method == "POST": 
     form = FooterForm(request.POST or None) 
     if form.is_valid(): 
      first_name = form.cleaned_data.get('first_name', '') 
      last_name = form.cleaned_data.get('last_name', '') 
      phone_number = form.cleaned_data.get('phone_number', '') 
      email_address = form.cleaned_data.get('email_address', '') 
      subject = 'New Mailing List Subscriber' 
      from_email = settings.DEFAULT_FROM_EMAIL 
      # admin_email = ['[email protected]'] 
      recipient_list = ['[email protected]', '[email protected]****.com', '[email protected]****.com'] 
      ctx = { 
       'subject': subject, 
       'first_name': first_name, 
       'last_name': last_name, 
       'phone_number': phone_number, 
       'email_address': email_address 
      } 

      agilecrm.create_contact(
       first_name=first_name, 
       last_name=last_name, 
       email=email_address, 
       company='', 
       custom={ 
        'WebForm': 'Footer Signup' 
       }, 
      ) 

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

     if form.errors: 
      json_data = json.dumps(form.errors) 
      return HttpResponseBadRequest(json_data, content_type='application/json') 
    else: 
     raise Http404 

    return HttpResponse(footer_form) 

回溯

Traceback (most recent call last): 
    File "/Users/rooster/Documents/Development/mylittlecarnival/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner 
    response = get_response(request) 
    File "/Users/rooster/Documents/Development/mylittlecarnival/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "/Users/rooster/Documents/Development/mylittlecarnival/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "/Users/rooster/Documents/Development/mylittlecarnival/home/views.py", line 42, in footer_form 
    'WebForm': 'Footer Signup' 
    File "/Users/rooster/Documents/Development/mylittlecarnival/venv/lib/python3.5/site-packages/agilecrm/__init__.py", line 99, in create_contact 
    result = json.loads(contact.content) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads 
    s.__class__.__name__)) 
TypeError: the JSON object must be str, not 'bytes' 
[01/Mar/2017 08:26:25] "POST /footer_form/ HTTP/1.1" 500 18849 
+1

這似乎是py-agilecrm庫中的一個錯誤 - 可能它與Python 3不兼容。(該代碼應該可能訪問'contact.json()',而不是使用'json.loads(contact。內容)' - 想做一個公關嗎?) –

+0

'contact.json()'做了竅門。我很想做一個公關 - 我會加入我的待辦事項。謝謝。 –

回答

0

這意味着你通過一些bytes參數(first_name, last_name, email_address)至agilecrm.create_contact
很容易找到它print,通過arg.decode('ENCODING_TYPE(eg: utf-8)')解決它。

相關問題