为什么我在Django中收到此错误(我正在尝试304未修改)

时间:2010-02-24 23:18:49

标签: python django http header http-status-code-304

def list_ajax(reqest):
    #q = request.GET.get('q',None)
    #get all where var = q.
    return ...
list_ajax = condition(etag_func=list_ajax)(list_ajax)

如您所见,如果结果相同,我正在尝试将304返回给客户端。但是,我收到这个Django错误,为什么?:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/http.py" in inner
  130.                 response['ETag'] = quote_etag(res_etag)
File "/usr/local/lib/python2.6/dist-packages/django/utils/http.py" in quote_etag
  118.     return '"%s"' % etag.replace('\\', '\\\\').replace('"', '\\"')

Exception Type: AttributeError at /list/ajax/
Exception Value: 'HttpResponse' object has no attribute 'replace'

编辑:我这样做了:

def etag_generate(p):
    thestring =  cPickle.dumps(p)
    return thestring

@etag(etag_generate)
def list_ajax(request):
    ...
    etag_generate(mydictresults)
    return render_to_response("list.html",mydictresults)

我将所有结果都转换为String,希望可以从这个字典生成一个哈希。但是,似乎@etag不会让我生成cPickle。错误是:

Exception Type: TypeError at /list/ajax/ 
Exception Value: can't pickle file objects

2 个答案:

答案 0 :(得分:0)

正确的etag_func会返回一些可序列化的数据。在你的情况下,最好的选择是这样的:

@etag(_get_list)
def list_ajax(request):
    objects = _get_list(request)
    return render_to_response("list.html", {"objects": objects})

def _get_list(request):
    q = request.GET["q"]
    # find and return records here
    # ...

答案 1 :(得分:0)

固定。

传递了请求。

def list_ajax_etag(request):
    return str(request.GET.get('l',''))+str(request.GET.get('a',''))