Avwerosuo

counting the number of posts a user has viewed

Mar 27th, 2020
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. Hello guys..... Please I need help
  2.  
  3. I have a one to one field connected to my User model,  named 'profile'
  4.  
  5. It has two fields
  6. 1. Image
  7. 2. Point (integer filled)
  8.  
  9. The point is visible in the user profile page... , my main aim is to let the user know the number of pages he or she has visited, it will be displayed in the user profile
  10. *I want the point filed to increase only once when a user visits a post_detauil.view URL/page,
  11. *i also want a button that can only be accessed by the admin to reset the value of the point filed to zero (0) for one and all the users users
  12.  
  13.  
  14.  
  15. ##################################
  16. #MOdels.py file of user app
  17. from django.db import models
  18. from django.contrib.auth.models import User
  19. # Create your models here.
  20.  
  21.  
  22. class profile(models.Model):
  23.     user = models.OneToOneField(User, on_delete=models.CASCADE)
  24.     image = models.ImageField(default='default.jpg', upload_to='profile_pics')
  25.     point = models.IntegerField(default=0)
  26.  
  27.  
  28.     def __str__(self):
  29.         return f'{self.user.username} profile'
  30.  
  31.  
  32.  
  33. ###############################
  34. #views.py file of user app
  35.  
  36. from django.shortcuts import render,redirect, get_object_or_404
  37. from django.contrib.auth.forms import UserCreationForm
  38. from . forms import UserRegistrationForm , UserUpdateForm, ProfileUpdateForm
  39. from . models import profile
  40. from django.contrib.auth.models import User
  41.  
  42. # Create your views here.
  43.  
  44.  
  45.  
  46. def point(request):
  47.     if request.method == 'POST':
  48.         user = get_object_or_404( User)
  49.         point = user.profile.objects.get(request.POST, instance = request.user)
  50.         point.point = point.point +2
  51.         point.save()
  52.  
  53.         return redirect('home')
  54.  
  55.  
  56. ###############################
  57. #urls.py file of blog app
  58. from django.urls import path
  59. from .views import PostListView, PostDetailView
  60. from . import views
  61.  
  62.  
  63. urlpatterns = [
  64.     path('', PostListView.as_view(), name='blog_home'),
  65.     path('post/<int:pk>/', PostDetailView.as_view(), name='post_detail'),
  66.     path('about/', views.about, name='blog_about'),
  67.  
  68.  
  69. ]
Advertisement
Add Comment
Please, Sign In to add comment