Not pure Django. Chrome versus Safari discrepancy

82 views
Skip to first unread message

Andrew Taylor

unread,
Feb 14, 2014, 6:55:03 AM2/14/14
to django...@googlegroups.com
Hi,

I have been working my way through the tango with django tutorial, which I have to say I have found to be excellent. 

There is one section (above link) whereby you track the number of times a url has been clicked. This is done by publishing links to an internal url, and routing that through a track/redirect view which converts and redirects to the real url and increments the page views at the same time.

However, safari was doing this on an inconsistent basis but seemed to work when the browser was first opened. After debugging this for a while I concluded the problem was not the code but my safari browser. It occurred to me that safari does this weird caching thing where you can go back/forward through pages immediately. On switching to Chrome the page counts increment as expected. I have tried setting 'disable caches' in the Develop menu in safari to no effect

Can someone with more experience than me explain why I am seeing these effects, and what a real world cross-browser solution to tracking page views would be?

The relevant code is below:

1. Track/redirect view:

def track_url(request):

    context = RequestContext(request)

    page_id = None

    url = '/rango/'

    if request.method == 'GET':

        if 'page_id' in request.GET:

            page_id = request.GET['page_id']

            try:

                page = Page.objects.get(id=page_id)

                page.views = page.views + 1

                page.save()

                url = page.url

            except:

                pass


    return redirect(url)


2. The internal url published in template:


{% if pages %}

<ul>

    {% for page in pages %}

    <li>

        <a href="/rango/goto/?page_id={{page.id}}">{{page.title}}</a>

        {% if page.views > 1 %}

            - ({{ page.views }} views)

        {% elif page.views == 1 %}

            - ({{ page.views }} view)

        {% endif %}

    </li>

    {% endfor %}

</ul>

{% else %}

<strong>No pages currently in category.</strong><br/>

{% endif %}



Thanks,


Andy

C. Kirby

unread,
Feb 14, 2014, 11:18:53 AM2/14/14
to django...@googlegroups.com
You can try using the @never_cache decorator on the view that captures the page view

from
django.views.decorators.cache import never_cache

That should instruct safari not to cache the page.
Sometimes that won't work due to how browsers respect the headers. You can also try the decorator:


from
django.views.decorators.cache import cache_control
@cache_control(no_cache=True, must_revalidate=True, no_store=True)

Andrew Taylor

unread,
Feb 15, 2014, 9:41:33 AM2/15/14
to django...@googlegroups.com
Thanks will experiment.
Reply all
Reply to author
Forward
0 new messages