Search code examples
djangoapachecachingsquid

Django is caching even though it shouldn't


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.


Solution

  • Using @never_cache decorator seems to solve the problem.