2016-07-29 5 views
1

内部アプリケーションの認証されたユーザーのページへの訪問を追跡できるようにするため、iveはhttps://djangopackages.org/grids/g/analytics/を見ていますが、要件を満たすものは表示されません。Django - 認証されたユーザーを追跡するページvistis

私が知る必要があるのは、私のauthenciatedユーザーが何回訪れているかということです。例えば

、またはsomethign似

User  | logins | total page visits | most visited url | last visited url 
John Smith | 100 | 2000    | sitedetails/1  | sitedetails/50 

おかげ

+0

http://stackoverflow.com/questions/2526966/count-number-of-logins-by-specific-user-django - >これはあなたのログインの数を与えますが、総ページ訪問のためにと他のフィールドでは、あなたのモデルを作成し、marcusshepの答えで指定されたものと同じビューのロジックを記述する必要があります –

答えて

1

私が知る必要があるすべては私のauthenciated、ユーザーが訪問し、どのように何回もされているものです。

最初に気になるのは、ユーザーに外部キーを割り当て、要求したビューのチャーフィールドを使用してモデルを作成することです。

class Request(models.Model): 
    user = models.ForeignKey(User) 
    view = models.CharField(max_length=250) # this could also represent a URL 
    visits = models.PositiveIntegerField() 

これにより、ユーザーがページをヒットした回数をカウントできるようになります。

def some_view(req, *a, **kw): 
    # try to find the current users request counter object 
    request_counter = Request.objects.filter(
     user__username=req.user.username, 
     view="some_view" 
    ) 
    if request_counter: 
     # if it exists add to it 
     request_counter[0].visits += 1 
     request_counter.save() 
    else: 
     # otherwise create it and set its visits to one. 
     Request.objects.create(
      user=req.user, 
      visits=1, 
      view="some_view" 
     ) 

あなたが1つのウェル書かれた関数にこのロジックを分離し、各ビューの初めにそれを呼び出すことができ、時間がかかる場合。

def another_view(req, *a, **kw): 
    count_request() # all logic implemented inside this func. 

また、クラスベースのビューでも可能です。

class RequestCounterView(View): 

    def dispatch(req, *a, **kw): 
     # do request counting 
     return super(RequestCounterView, self).dispatch(*args, **kwargs) 


class ChildView(RequestCounterView): 
    def get(req, *a, **kw): 
     # continue with regular view 
     # this and all other views that inherit 
     # from RequestCounterView will inherently 
     # count their requests based on user. 
関連する問題