500服务器错误,我不知道哪里出错,ajax,django
我有一些代码,已经搞了好几个小时了,但就是搞不明白哪里出问题了。
我一直收到500的服务器错误响应,而且在我触发ajax的时候,连调试视图定义都无法开始。
我真的很困惑,任何帮助都非常感谢!
$('.cheque_info_edit_button').live('click', function(){
var new_cheque = {
// cheque number is taken from the cell, not input box for this one.
cheque_no: $(this).closest('td').closest('tr').find('.inv_prof_cheque_no').text(),
their_bank: $(this).closest('td').closest('tr').find('.new_their_bank_input_ajax').val(),
our_bank: $(this).closest('td').closest('tr').find('.new_our_bank_input_ajax').val(),
cash_in_date: $(this).closest('td').closest('tr').find('.new_cash_in_date_input_ajax').val(),
cheque_amount: $(this).closest('td').closest('tr').find('.new_cheque_amount_input_ajax').val(),
info_type: 'edit'
};
var cheque_json = JSON.stringify(new_cheque);
$.ajax({
type: 'POST',
url: '/best_choose/invoice/profile/inv_add_or_edit_cheque/',
data: cheque_json,
success: function(){
// do stuff
}
更新:我觉得我的视图在语法上没有问题,所以我把它去掉了,添加了错误追踪信息,是不是csrf令牌出了问题?我其他的ajax功能都正常。
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/best_choose/invoice/profile/inv_add_or_edit_cheque/
Django Version: 1.3
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'SY_SYSTEM.sy_system',
'django.contrib.humanize']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
178. response = middleware_method(request, response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/middleware/csrf.py" in process_response
287. response.content, n = _POST_FORM_RE.subn(add_csrf_field, response.content)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/__init__.py" in _get_content
596. return smart_str(''.join(self._container), self._charset)
Exception Type: TypeError at /best_choose/invoice/profile/inv_add_or_edit_cheque/
Exception Value: sequence item 0: expected string, NoneType found
5 个回答
0
当请求不是通过ajax发起的时候,试着进行回滚操作。使用commit_manually这个装饰器时,你需要选择回滚或者提交。
1
从我看到的错误信息来看:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/__init__.py" in _get_content
596. return smart_str(''.join(self._container), self._charset)
Exception Type: TypeError at /best_choose/invoice/profile/inv_add_or_edit_cheque/
Exception Value: sequence item 0: expected string, NoneType found
显示出 ''.join(self._container)
这一部分遇到了一个不是字符串的元素(NoneType),也就是说,表单中的某个部分变成了 NoneType,导致在添加 CSRF 字段时无法正确处理。根据 Django 的代码,self._container
实际上是传入的 HttpResponse 的内容。这意味着 Django 正在尝试在响应的表单中添加 csrfmiddletoken 字段,而用来匹配表单并插入这个字段的正则表达式,结果却捕获到了一个 NoneType,而不是字符串。
这里提到的正则表达式是:
_POST_FORM_RE = \
re.compile(r'(<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE)
我建议你检查一下你生成的响应表单,确保它的格式是正确的。逐步查看 CSRF 相关的代码,看看到底是在哪个地方捕获到了 NoneType 也会有帮助。
如果不查看视图处理程序和表单代码,我也没法说得更多。
4
我看了你的jQuery代码,应该没有什么错误...
这只是我的一个猜测,因为之前我也遇到过类似的情况,花了我一段时间才意识到(忘记了错误信息是什么样的)
你可能需要检查一下你的网址文件,确保你没有使用相同的网址或者已经存在的网址模式的通配符。