user6841991
user6841991

Reputation:

Django form not saving data to database

I am making an app in django where users are asked a few questions about a business and their answers are stored in the database. I have registered the app in the admin section and can see it in the admin panel.

However while submitting the form, the data doesn't seem to saved in the database although url review-sucess is shown. The admin section doesn't show any records inserted.

models.py

from django.db import models

yes_or_no_choices = (
    ('YES','Yes'),
    ('YES','No'),
)

class ModelReview(models.Model):
    review_date                  = models.DateTimeField()
    business_name                = models.CharField(max_length=200)
    user_review                  = models.TextField()
    user_name                    = models.CharField(max_length=50)
    photos                       = models.ImageField()

forms.py

from django import forms
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _

from . import models

class FormReview(forms.ModelForm):
    class Meta:
        model = models.ModelReview
        fields = '__all__'
        exclude = ['review_date']

views.py

from django.http import HttpResponse
from django.shortcuts import render, redirect
from . import forms


def FunctionReviewSuccess(request):
    return render(request, 'reviews/review-success.html')

def FunctionPostReview(request):
    if request == 'POST':
        instance_form = forms.FormReview(request.POST.request.FILES or None)
        if form.is_valid:
            form.save()
            return redirect ('review-sucess')

    else:
        instance_form = forms.FormReview()
    return render(request, 'reviews/post-review.html',{'prop_form':instance_form })

post-review.html

<h1>Post a review</h1>
    <form class="review-form" action="{% url 'review-success' %}" method="post" enctype="multipart/form-data">
      {% csrf_token %}
      {{ prop_form }}
      <input type="submit" name="" value="Post Review">
    </form>

Upvotes: 1

Views: 5857

Answers (2)

Ashish Nautiyal
Ashish Nautiyal

Reputation: 931

Please check below points:

On Submit action is defined to success instead of view that is handling the post request.

as mentioned by @Daniel Roseman your code is having issues please rectify them.

One thing you can do is before calling form.save method you can print the values out of cleaned data dict on your console and check for any discrepancy in data.

if form.is_valid:
    business_name= form.cleaned_data['business_name']
    print(business_name);
    form.save()

Also check for the review_date default value as you have not included the same in form.

review_date = models.DateTimeField()

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599450

You're posting your form directly to the FunctionReviewSuccess view, which doesn't do anything with the data. Your form needs be processed by FunctionPostReview, which is the same view as the one that displays the form in the first place. So post back to the same place:

<form class="review-form" action="." method="post">

Note, you don't have any files in your form, so you can omit the enctype.

Note further there are a large number of issues with your view:

  • you need to check request.method, not just request;
  • you call the form instance_form but then check form;
  • is_valid is a method not a property;
  • you have a dot rather than a comma between the POST and FILES arguments;
  • you shouldn't accept request.FILES anyway for the same reason as above;
  • you shouldn't use or None when you're already within the POST block.

So:

if request.method == 'POST':
    instance_form = forms.FormReview(request.POST)
    if instance_form.is_valid():
        instance_form.save()
        return redirect('review-sucess')

Finally, Python style is to use lower_case_with_underscore for function names such as views.

Upvotes: 5

Related Questions