Data is not getting stored in a database through a customer form

Viewed 90

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" class="needs-validation mt-3" style="font-size: 13px;" novalidate="" autocomplete="off">
    {% csrf_token %}

    <div class="radio-toolbar d-flex">
        <input type="radio" id="one_star" name="product_rating" value="1" checked>
        <label for="one_star" class="mx-1">1 <i class="fas fa-star"></i></label>

        <input type="radio" id="two_star" name="product_rating" value="2">
        <label for="two_star" class="mx-1">2 <i class="fas fa-star"></i></label>

        <input type="radio" id="three_star" name="product_rating" value="3">
        <label for="three_star" class="mx-1">3 <i class="fas fa-star"></i></label>

        <input type="radio" id="four_star" name="product_rating" value="4">
        <label for="four_star" class="mx-1">4 <i class="fas fa-star"></i></label>

        <input type="radio" id="five_star" name="product_rating" value="5">
        <label for="five_star" class="mx-1">5 <i class="fas fa-star"></i></label>
    </div>

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

Since, you are not doing anything with FeedBackForm, so you can simply get the names of fields through POST request in view as:

from django.shortcuts import redirect
def quick_view(request, quick_view_id):
    if request.method == "POST" and request.user.is_authenticated:
        ProductREVIEWS.objects.create(
            feedBACK=request.POST.get('feedBACK'),
            rating=request.POST.get('product_rating')
        )
        return redirect('success')
    return render(request, 'some_folder_name/index.html')


def success(request):
    return render(request, 'some_folder_name/success.html')

Note: Its a good practice to redirect after dealing with POST data.

Template file:

<form method="POST" class="needs-validation mt-3" style="font-size: 13px;"   autocomplete="off">
    {% csrf_token %}

    <div class="radio-toolbar d-flex">
        <input type="radio" id="one_star" name="product_rating" value="1" checked>
        <label for="one_star" class="mx-1">1 <i class="fas fa-star"></i></label>

        <input type="radio" id="two_star" name="product_rating" value="2">
        <label for="two_star" class="mx-1">2 <i class="fas fa-star"></i></label>

        <input type="radio" id="three_star" name="product_rating" value="3">
        <label for="three_star" class="mx-1">3 <i class="fas fa-star"></i></label>

        <input type="radio" id="four_star" name="product_rating" value="4">
        <label for="four_star" class="mx-1">4 <i class="fas fa-star"></i></label>

        <input type="radio" id="five_star" name="product_rating" value="5">
        <label for="five_star" class="mx-1">5 <i class="fas fa-star"></i></label>
    </div>

    <div class="mb-3 mt-2">
        <textarea id="email" placeholder="Share your experiencs..." rows="10" style="font-size: 13px; text-transform: lowercase;" type="email" class="form-control" name="feedBACK" value="" required></textarea>
        </div>
        <input type="submit" value="save">
    </form>

urls.py

urlpatterns = [
    ...
    path('success/',views.success,name='success')
]

success.html

<body>
    <h2>Form successfully submitted.</h2>
</body>

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.

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