How to track and manage pages a user visits in Django (History App).
HTML-код
- Опубликовано: 10 сен 2024
- Learn how to track and manage the pages a user views in your django project.
Data is becoming an integral part in building modern systems and applications. This type of data can be used to create Recommender Systems using machine learning.
I urge you to think of different types of data you could collect (remember to tell your users that you collect some data in the EUA) to enhance your user experience.
We would be the using the contentype app from django to achieve this.
Source code: github.com/fle...
Link to my previous tutorial: • Prevent Multiple Devic...
Here's a link to the contenttypes documentation: docs.djangopro...
Thank you so much. Very clear tutorial
I'm glad you liked it
The video on preventing multiple logins was a success!! @Emmanuel Okiche.. So, you were saying?? About tracking how many times a visitor has bought something from my site and to notify him/her after, say, 5 transactions, if he/she wants to be a permanent member.
How did you make history app?
@Emmanuel Okiche, does this mean I have to build a History App first and only then will I be able to make use of recommendations and suggestions using Machine Learning?
No. You must not build a history app first. There are different ways to achieve this but most importantly, you have to feed your recommendation system some data for it to give recommendations. This data could be stored could be product ids stored somewhere maybe in a session, localstorage or in the db just like the history app.
Genuine question: why use Signals and custom Mixins for this example? Instead of importing the custom mixin across apps, you could just import the History model itself, and create instances of it as needed inside the get_object() method of your class-based views. This way, you don't need to use Signals (which are arguably problematic, anyway) and you also don't need to create a custom Mixin... Seems easier / cleaner to me, unless I'm missing something? The only 'required' thing here is ContentTypes + Generic ForeignKey; the rest seems more complex than actually necessary, unless you are specifically avoiding importing the History model into your other apps for some reason (it wouldn't be a circular import, since the History model doesn't import any other models, the whole point being that its relations will be 'generic'....)
Thanks for the question.
My tutorial showed you one way of the doing it.
Your comment shows another way of doing it.
I prefer my method and i have no issues with signals.
Also, we think differently and this is what i came up with.
Thanks for the comment.
I'm glad the video made you think of other ways you could achieve this.
All the best :]
hi i am getting error
in blog detail page when user not logged in
TypeError at /blog/What is Lorem Ipsum
'AnonymousUser' object is not iterable
how to avoid this?
At 16:16 i added a check in the mixin for authenticated users so you shouldn't get that error.
Try cross check your code.
Thank you so much Emmanuel
You're welcome. Don't forget to share and subscribe. More django tutorials coming soon.
Nice job!
Thanks!
I'm glad you liked it
can i use this to keep track of data edited in a model as record to view no. of times edited and also the previos state after editing?
Hmmm...you can use some of the logic to store the num of times edited but a simpler way would be to add a field to the model that tracks th e edit count ans increment it whenever the data changes.
i think the Django admin keeps track of the previous state.
The previous state logic would be something like making a post model something like a category model.
So every new edit is a new insert into the post group.
So the edit count would be total post in the category - 1
Check to confirm if Django admin does this.
Could you make a tutorial on how to track the number of times an IP address has visited my site? Is there a plugin for this or do I have to create a Python file that manually does this then passes data as a dictionary to the views? The idea in mind is to prompt the user if they wanna be a permanent member, after the user has, say, bought several items from my site.
My tutorial on preventing multiple user login could give you an idea on how to achieve this.
The main idea is to have a model that stores the ip then you use a middleware to check if the current ip is in your db then increment the count.
Just checkout the tutorial. I believe it would be helpful for this.
All the best
@@fleepgeek Thanks bro. I've already downloaded it and will watch it as soon as I'm done with Django Auth customization. I'll give you the feedback!
@@fleepgeek Okay, now it's time to put this to the test. It totally makes sense now that I've seen the video on Preventing Multiple Device Login.
how could I track ListView using mixins like what you do in this tuto and it is a great tuto. and there is a way tou get same result using views based on functions?
Maybe decorators could do it but i haven't tried it out.
how i can use it with function base view?
You could use decoratiors instead of mixins.
Please use larger fonts. It is really hard to read. I'm not a fan of dark mode, but it would be better if the fonts were larger.
Thanks for the feedback. Would work on that :]
@Emmanuel Okiche Thanks for this tutorial it really helped me a lot on a very important project. However, I am facing a problem. I am able to see the History page on admin, but it keeps remaining empty. You included ObjectViewMixin on Post Detail View. However my project’s views are all FBV and I am not able to change them because of the concept of the website. Could you please help me to solve this problem? How could I make this tutorial work on my FBV? Thanks a lot!
I'm glad you found it helpful.
You could use a decorator instead of a mixin to achieve the same effect using a FBV.
Just study how i implementated the mixin and use the same concept for the decorator.
Secondly, a CBV can always replace a FBV no mattet the concept of the site.
You just have to dig deep and understand how CBV works.
I have a CBV course on my channel.
All the best
code is not working, no records r showing in history...:(
Code is working, records r showing in history...lol
Okay seriously, you're might be doing something wrong.
Did you follow the video step by step?
@@fleepgeek no, i run the downloaded code as it is. Sir can uh suggest the possible mistake , that i have made.
@@iramshahzadi4233 yes of course. I'm here to help.
The mistake you made was refusing to watch the video and follow step by step.
source code please
github.com/fleepgeek/django-history-app