I'm currently trying to make a form that adds the value to the"point" model that I created but it seems to not go through. I made a form that should allow the user to put any integer value and it should add (or subtract) to the model. Can anyone point me to the right direction on what to do? Any help is appreciated.
Here is my forms.py:
class addpointForm(forms.ModelForm):
add_point_field = forms.IntegerField(widget=forms.NumberInput)
class Meta:
model = Points
fields = ['user']
Model.py:
class Points(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
points = models.IntegerField(default=0, null=False)
def __str__(self):
return self.user.username
views.py:
@login_required
def pointform(request):
if request.method=='POST':
form = addpointForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
messages.success(request, 'Success! Points has been added!')
instance.user.points += addpointForm.add_point_field
form.save()
else:
messages.error(request, 'Oh no! There was an error when you were adding points!')
form = addpointForm()
return render (request,'users/addpoints.html',{'form':form})