I have a website programmed in Django, and as part of the website, I have a view counter for individual pages. However, since I test-view my own files rather often, I don't want to count views that I give to my own pages.

Right now, all I do is that when a specific page is requested, it simply updates a "views" variable in the model for that page by increasing it by 1. This is okay for my own purposes - I don't mind recording multiple views by the same person - but I simply don't want my own views to count.

What ways are there to do this? Please advise.

有帮助吗?

解决方案

You can set a temporary cookie variable in your browser.

For example, in Chrome, you can use the following or the Resources tab on the Web Inspector:

  1. browse to website
  2. In browser URL bar, type: javascript:document.cookie="my_app_who_am_i=itsa_me_mario"

and pull it back using some django:

request.COOKIES.get('my_app_who_am_i') 

Or if you had sessions setup already on your web server, you could set the cookie only when your account logs in.

response = render_to_response(template_name, context)
response.set_cookie('my_app_who_am_i', 'itsa_me_mario') 
return response

其他提示

You could use session for it. And when the session is for example, admin(you) then, don't count it.

You can add a cookie like "ignore_view" and if the cookie is present you don't increase the counter.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top