1

I have created form using ModelForm but its not saving data into database.

views.py

def answer(request):
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = AnswerForm()

    return render_to_response('quiz/index.html', {'form': form, })

template

<form action="." method="post">
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

model

class Answer(models.Model):
    answer = models.TextField()

class AnswerForm(ModelForm):
    class Meta:
        model = Answer

Where i was wrong ? :/

1
  • 1
    Check if your form is really valid. Does form.is_valid() returns True? Commented Feb 5, 2012 at 4:50

1 Answer 1

1

You forgot to handle the case where the form isn't valid.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain it ? :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.