Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello guys..... Please I need help
- I have a one to one field connected to my User model, named 'profile'
- It has two fields
- 1. Image
- 2. Point (integer filled)
- 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
- *I want the point filed to increase only once when a user visits a post_detauil.view URL/page,
- *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
- ##################################
- #MOdels.py file of user app
- from django.db import models
- from django.contrib.auth.models import User
- # Create your models here.
- class profile(models.Model):
- user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
- image = models.ImageField(default='default.jpg', upload_to='profile_pics')
- point = models.IntegerField(default=0)
- # FIX added related_name
- def __str__(self):
- return f'{self.user.username} profile'
- ###############################
- #views.py file of user app
- from django.shortcuts import render,redirect, get_object_or_404
- from django.contrib.auth.forms import UserCreationForm
- from . forms import UserRegistrationForm , UserUpdateForm, ProfileUpdateForm
- # FIXME you might need to remove this profile import here if not used elsewhere
- from . models import profile
- from django.contrib.auth.models import User
- # Create your views here.
- # FIXED
- def point(request):
- if request.method == 'POST':
- user = get_object_or_404(User, request.user)
- point = user.profile.point
- point += 2
- point.save()
- return redirect('home')
- ###############################
- #urls.py file of blog app
- from django.urls import path
- from .views import PostListView, PostDetailView
- from . import views
- urlpatterns = [
- path('', PostListView.as_view(), name='blog_home'),
- path('post/<int:pk>/', PostDetailView.as_view(), name='post_detail'),
- path('about/', views.about, name='blog_about'),
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement