Hi,

I have been working my way through the tango with django tutorial, which I 
have to say I have found to be excellent. 
http://www.tangowithdjango.com/book/chapters/tango_too.html

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/98ed0f4a-d93f-404b-8957-aaaedb06109f%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to