开发者

Why am I getting this error in Django (I'm trying to do a 304 not modified)

开发者 https://www.devze.com 2022-12-21 00:14 出处:网络
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)
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)

As you can see, I'm trying to return a 304 to the client if the result is the same. But, I am getting this Django error, why?:

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'

Edit: I did this:

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)

I'm turning all the results into a String, hoping that a hash could be generated from this dictionary. But, it seems like the @etag won't let me generate the cPickle. Error is:

Exception Type: TypeError at /list/ajax/ 
Exception Value: can't pickle f开发者_如何学JAVAile objects


The correct etag_func would return some serializable data. In your case, the best choice is something like this:

@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
    # ...


Fixed.

Passed the request in.

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

精彩评论

暂无评论...
验证码 换一张
取 消