Django 正在缓存,尽管它不应该

发布于 2024-10-15 18:08:43 字数 1673 浏览 5 评论 0原文

我有以下问题。在我的应用程序中,我允许用户将图像上传到我的静态服务器。每个图像都有一个在每次刷新视图时生成的唯一 ID。这部分应用程序通过 Squid 和 Apache 进行代理。但在我的管理部分(没有代理)我也可以使用相同的代码上传文件。奇怪的是,我的唯一 ID 被缓存在这里,并在 apache 清除 python 缓存时约 15 分钟后发生变化。以前有人遇到过这样的问题吗?

这里有一些代码,上传表单:

    <form method="post" target="upload_target" action="{{ upload_url }}" enctype="multipart/form-data">
        {{ form.as_p }}
        <p style="display:none">
            <input type="hidden" maxlength="64" name="myfileid" id="myfileid" value="{{ myfileid }}" >
        </p>
        <p>
            <input id="submit" type="submit" value="Add">
        </p>
        <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
    </form>

视图:

def manage_element_image(request, object_id):

    template = 'management/add_element.html'

    upload_url = settings.STATIC_SERVER
    form = ElementImageForm()   
    myfileid = create_did()

    try:
        object = Element.objects.get(id=int(object_id))
    except:
        object = None

    result = render_to_string(template, RequestContext(request, {
        'upload_url': upload_url,
        'form': form,
        'myfileid': myfileid,
        'objectid': object_id,
        'object': object,
    }))

    return HttpResponse(result)

和生成 id 的函数:

def create_id():

    hash = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789"
    length = 43
    prefix = "ELEMENT"
    number = ""
    for i in range(0, length):
        number += random.choice(hash)

    return prefix + number

我正在使用虚拟缓存,但仅用于查询。

I have the following problem. In my application I allow users to upload images to my static server. Each image has a unique ID generated on each refresh of the view. This part of the application is proxied through Squid and Apache. But in my management part (which is not proxied) I also have ability to upload files, using just the same code. And strangely my unique ID is being cached here and changes after ~15min when apache clears python cache. Has anybody encountered such problem previously ?

Some code here, upload form :

    <form method="post" target="upload_target" action="{{ upload_url }}" enctype="multipart/form-data">
        {{ form.as_p }}
        <p style="display:none">
            <input type="hidden" maxlength="64" name="myfileid" id="myfileid" value="{{ myfileid }}" >
        </p>
        <p>
            <input id="submit" type="submit" value="Add">
        </p>
        <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
    </form>

view :

def manage_element_image(request, object_id):

    template = 'management/add_element.html'

    upload_url = settings.STATIC_SERVER
    form = ElementImageForm()   
    myfileid = create_did()

    try:
        object = Element.objects.get(id=int(object_id))
    except:
        object = None

    result = render_to_string(template, RequestContext(request, {
        'upload_url': upload_url,
        'form': form,
        'myfileid': myfileid,
        'objectid': object_id,
        'object': object,
    }))

    return HttpResponse(result)

and function generating id :

def create_id():

    hash = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789"
    length = 43
    prefix = "ELEMENT"
    number = ""
    for i in range(0, length):
        number += random.choice(hash)

    return prefix + number

I'm using dummy caching but only for queries.

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

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

发布评论

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

评论(1

野味少女 2024-10-22 18:08:43

使用 @never_cache 装饰器似乎可以解决这个问题。

Using @never_cache decorator seems to solve the problem.

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