Home > database >  Update form store none value when I submit the update form. How can I fix It?
Update form store none value when I submit the update form. How can I fix It?

Time:07-22

I made a feedback form (def feedBack) so that a user can give feedback. It's working well. Now my motive to create an update form so that a user can be able update their feedback. I also have written a view for update feedback (def UpdateFeedback). But it's not working perfectly. When I submit the update form, then it updates none. Where did the actual problem occur?

views.py:

This view for storing feedback and it's working well.

def feedBack(request,quick_view_id):
    quick_view = get_object_or_404(Products, pk=quick_view_id)
    if request.method == "POST" and request.user.is_authenticated:

        try:
            ProductREVIEWS.objects.create(
                user=request.user,
                product=quick_view,
                feedBACK=request.POST.get('feedBACK')
            )
            return redirect('quick_view', quick_view_id)
        except:
            return redirect('quick_view', quick_view_id)

    else:
        return redirect('quick_view', quick_view_id)

this view for update the feedback, but it's store none

def UpdateFeedback(request, id):
    feedback = ProductREVIEWS.objects.get(pk=id)
    product_id = feedback.product.id
    reviewers = feedback.user
    
    if request.method == "POST":
        form = UpdateFeedbackForm(request.POST)

        if form.is_valid() and reviewers.id == request.user.id:
            
            UpdateFeedbackForm(request.POST)
            feedBACK = form.cleaned_data.get('UpdateFeedBACK')
            feedback.feedBACK = feedBACK
            feedback.save()
            messages.success(request, "Feedback is updated")
    
    return redirect('quick_view', product_id)

forms.py:

class UpdateFeedbackForm(forms.ModelForm):

    class Meta:
        model = ProductREVIEWS
        fields = ('feedBACK')
        
        labels = {
            'feedBACK':'Change Your View'
        }

        widgets = {
            'feedBACK':forms.Textarea(attrs={'class':'form-control', 'style':'font-size:13px;'})
        }

models.py:

class ProductREVIEWS(models.Model):

    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='userREVIEW',on_delete=models.CASCADE)
    product = models.ForeignKey(Products, related_name='productREVIEWrelatedNAME',on_delete=models.CASCADE)
    feedBACK = models.TextField(blank=True, null=True)

urls.py:

path("feedBack/<int:quick_view_id>/", views.feedBack, name="feedBack"),
path("UpdateFeedback/<int:id>/", views.UpdateFeedback, name="UpdateFeedback")

template:

{% for feedBack in AllProductFeedback %}
<form action="{% url 'UpdateFeedback' id=feedBack.id %}" method="POST" enctype="multipart/form-data">
    {% csrf_token %}

    <textarea id="UpdateFeedBack" rows="6" style="font-size: 13px;"  name="UpdateFeedBACK" value="" required>{{feedBack.feedBACK}}</textarea>

</form>
{% endfor %}

CodePudding user response:

def UpdateFeedback(request, id):
    feedback = ProductREVIEWS.objects.get(pk=id) #  I suggest you to handle DoesNotExist exception case.

    product_id = feedback.product.id
    reviewers = feedback.user
    
    if request.method == "POST":
        form = UpdateFeedbackForm(request.POST, instance=feedback) #  you need to pass instance here

        if form.is_valid() and reviewers.id == request.user.id:
            
            UpdateFeedbackForm(request.POST) #  doesn't do anything here, return value ignored.
            feedBACK = form.cleaned_data.get('UpdateFeedBACK') #  Will return None, since there is no UpdateFeedBACK in form fields.
            # So you can remove two lines above.
            form.save() #  This will update the ProductREVIEWS model
            messages.success(request, "Feedback is updated")
    
        return redirect('quick_view', product_id)

CodePudding user response:

def UpdateFeedback(request, id):
    feedback = ProductREVIEWS.objects.get(pk=id)
    product_id = feedback.product.id
    reviewers = feedback.user
    
    if request.method == "POST":
        form = UpdateFeedbackForm(request.POST)

        if form.is_valid() and reviewers.id == request.user.id:
            
            UpdateFeedbackForm(request.POST)
            feedBACK = form.cleaned_data.get('UpdateFeedBACK') #this will not work since there is no updatefeedback field in your form
            feedback=form.save(commit=False)
            feedback.feedBACK = feedBACK #(this feedBACK need to be object of your Products model)
            feedback.save()
            messages.success(request, "Feedback is updated")
    
    return redirect('quick_view', product_id)
  • Related