SHOW:
|
|
- or go back to the newest paste.
| 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) |
| 23 | + | user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile") |
| 24 | image = models.ImageField(default='default.jpg', upload_to='profile_pics') | |
| 25 | point = models.IntegerField(default=0) | |
| 26 | # FIX added related_name | |
| 27 | ||
| 28 | ||
| 29 | def __str__(self): | |
| 30 | return f'{self.user.username} profile'
| |
| 31 | ||
| 32 | ||
| 33 | ||
| 34 | ############################### | |
| 35 | #views.py file of user app | |
| 36 | ||
| 37 | from django.shortcuts import render,redirect, get_object_or_404 | |
| 38 | from django.contrib.auth.forms import UserCreationForm | |
| 39 | from . forms import UserRegistrationForm , UserUpdateForm, ProfileUpdateForm | |
| 40 | # FIXME you might need to remove this profile import here if not used elsewhere | |
| 41 | from . models import profile | |
| 42 | from django.contrib.auth.models import User | |
| 43 | ||
| 44 | # Create your views here. | |
| 45 | ||
| 46 | ||
| 47 | # FIXED | |
| 48 | - | user = get_object_or_404( User) |
| 48 | + | |
| 49 | - | point = user.profile.objects.get(request.POST, instance = request.user) |
| 49 | + | |
| 50 | - | point.point = point.point +2 |
| 50 | + | user = get_object_or_404(User, request.user) |
| 51 | point = user.profile.point | |
| 52 | point += 2 | |
| 53 | point.save() | |
| 54 | ||
| 55 | return redirect('home')
| |
| 56 | ||
| 57 | ||
| 58 | ############################### | |
| 59 | #urls.py file of blog app | |
| 60 | from django.urls import path | |
| 61 | from .views import PostListView, PostDetailView | |
| 62 | from . import views | |
| 63 | ||
| 64 | ||
| 65 | urlpatterns = [ | |
| 66 | path('', PostListView.as_view(), name='blog_home'),
| |
| 67 | path('post/<int:pk>/', PostDetailView.as_view(), name='post_detail'),
| |
| 68 | path('about/', views.about, name='blog_about'),
| |
| 69 | ||
| 70 | ||
| 71 | ] |