0

In my previous question, I recently asked how to make forms.py in Django 1.9 show in HTML. now that this is done im trying to make a button which when the selection has been made (in this case it's radiobuttons) it will post to the database and move on with the questionaire.

Currently im attempting to make it post in my views.py but im having no luck in making it send the data.

def question1(request):
    question_form = QuestionForm()
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            return render(request, 'music.questions2,html')
    return render(request, 'music/question1.html', locals())

Would really appreciate the help in making this happen.

10
  • Is it really radio buttons or multiple buttons? Remember: radio button allow user to select ony one whereas multiple allows... multiple ones! Commented Mar 7, 2017 at 15:38
  • @nik_m Yes i saw you had Checkboxes for the solution you gave me but Radiobuttons better suited my needs. (not to take away from your help with my query) Commented Mar 7, 2017 at 15:39
  • OK! No problem! As both answers bellow, use form.save(). It implies to have a form inherit from ModelForm, so that is associatied with a Model. Otherwise, Django will be unable to save the data since it will not know the "connection". Commented Mar 7, 2017 at 15:42
  • Okay, That makes sense so the Database is unaware of how to store that information so i need to make a model. models.py to handle the upload? if i understand you correctly? Commented Mar 7, 2017 at 15:48
  • Couldn't say it better! More on them here Commented Mar 7, 2017 at 15:49

2 Answers 2

1
def question1(request):
    question_form = QuestionForm()
    if request.method == 'POST':
        form = QuestionForm(request.POST)
            if form.is_valid():
                form.save()  # save to db!
                return render(request, 'music.questions2,html')
    return render(request, 'music/question1.html', locals())

# models.py
class Question(models.Model):
    # Q_CHOICES is the previous declared one
    question = models.CharField(max_length=20, choices=Q_CHOICES)

# forms.py
class QuestionForm(forms.ModelForm):
        class Meta:
            model = Question
            fields = ['question']
            widgets = {
                'question': forms.RadioSelect()
            }
Sign up to request clarification or add additional context in comments.

Comments

1

Use: form.save()

def question1(request):
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'music.questions2,html')
    else:
        form = QuestionForm()
    return render(request, 'music/question1.html', locals())

Comments

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.