2017-03-01 75 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)')解决它。

相关问题