Home > Software engineering >  Data is not getting stored in a database through a customer form
Data is not getting stored in a database through a customer form

Time:07-13

I made a customer feedback form with rating stars. But problem is, not getting stored data in database. Where the actual problem occured? What is the relevent solution? I tried many time.

forms.py:

class FeedBackForm(forms.Form):
    feedBACK = forms.CharField(widget=forms.Textarea, required=True)     
    rating = forms.CharField(widget=forms.NumberInput) 

models.py:

class ProductREVIEWS(models.Model):

rating = models.IntegerField(blank=True, null=True)
feedBACK = models.TextField(blank=True, null=True)

views.py:

def quick_view(request, quick_view_id):
    form = FeedBackForm()
    if request.method == "POST" and request.user.is_authenticated:
        form = FeedBackForm(request.POST)
        if form.is_valid():
            ProductREVIEWS.objects.create(
                feedBACK=form.cleaned_data.get('feedBACK'),
                rating=form.cleaned_data.get('product_rating'),
            )

template:

<form action="#!" method="POST"  style="font-size: 13px;" novalidate="" autocomplete="off">
    {% csrf_token %}

    <div >
        <input type="radio" id="one_star" name="product_rating" value="1" checked>
        <label for="one_star" >1 <i ></i></label>

        <input type="radio" id="two_star" name="product_rating" value="2">
        <label for="two_star" >2 <i ></i></label>

        <input type="radio" id="three_star" name="product_rating" value="3">
        <label for="three_star" >3 <i ></i></label>

        <input type="radio" id="four_star" name="product_rating" value="4">
        <label for="four_star" >4 <i ></i></label>

        <input type="radio" id="five_star" name="product_rating" value="5">
        <label for="five_star" >5 <i ></i></label>
    </div>

    <div >
        <textarea id="email" placeholder="Share your experiencs..." rows="10" style="font-size: 13px; text-transform: lowercase;" type="email"  name="feedBACK" value="" required></textarea>
     </div>

CodePudding user response:

Your code isn't actually utilising the functionality of Django forms. Firstly, I would suggest using a model form rather than just a normal form - it will automatically do some of the lifting to connect the form with your model.

https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/#modelform

Secondly, you are validating the form using is_valid() but you are then manually creating the object - you can instead using save() to create the model.

CodePudding user response:

You can use Create view

Django createview is displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object.

from django.views.generic.edit import CreateView

Class Post_Form(CreateView):
   model = ProductREVIEWS
   fields = ['rating', 'feedBack']
   secces_url = ('#your success url)
   template_name = '#your template'

Add this in the template you want to display the form:

<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
  • Related