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

发布于 2024-08-23 05:39:01 字数 1314 浏览 4 评论 0原文

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)

我将所有结果转换为字符串,希望可以从该字典生成哈希。但是,@etag 似乎不允许我生成 cPickle。错误是:

Exception Type: TypeError at /list/ajax/ 
Exception Value: can't pickle file objects
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 file objects

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-08-30 05:39:01

正确的 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
    # ...

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
    # ...
各自安好 2024-08-30 05:39:01

固定的。

把请求传了进去。

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

Fixed.

Passed the request in.

def list_ajax_etag(request):
    return str(request.GET.get('l',''))+str(request.GET.get('a',''))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文