RadioSelect form not getting rendered

I’m making quiz app and this is an answer form with input type radio, but it’s not working(there’s no error or anything). can somebody help me fix it or suggest a different approach?
forms:
from django import forms

ANSWER_CHOICES = [
    ('1', 'First'),
    ('2', 'Second'),
    ('3', 'Third'),
]
class AnswerForm(forms.Form):
    answer = forms.ChoiceField(
        required=False,
        widget=forms.RadioSelect,
        choices=ANSWER_CHOICES,
    )

views:
def answers(request):
if request.method == ‘POST’:
form = AnswerForm(request.POST)
if form.is_valid():
return HttpResponseRedirect(’/thanks/’)
else:
form = AnswerForm()

        return render(request, 'pages/questions.html', {'form': form})

html:
{% extends “main.html” %}

{% block content %}
  {% for Questions in question_list %}
    <h1>{{ Questions.title }}</h1>
    <form action="/yes/" method="post">
      {% csrf_token %}
      {{ form.as_p }}
    </form>
  {% endfor %}
{% endblock content %}

What do you mean it’s “not working”? What isn’t happening that you’re expecting to have happen, or what is happening that you’re not expecting to see?

Also, when posting code here, please enclose it between lines of three backtick - ` characters to preserve the formatting of the code. This means you’ll have a line of ```, then the lines of code, then another line of ```.

sorry this is my first time posting here. So those answers should get rendered on my html page(at least that’s what I intended) but it’s just other stuff I added and there is no form.

In your template you have:

But you’re not supplying any “question_list” in your context to be rendered.

I just didn’t include that. here it is:
def index(request):
question_list = Questions.objects.all()
context = {
‘question_list’: question_list
}
return render(request, ‘pages/questions.html’, context)

sorry I can’t mark it as a code. I don’t know why I did four spaces before the snippet

Don’t try to do the four spaces

And please include the complete view that you’re asking about here - this view is a different view from what you posted in your original post.

from django.http import HttpResponseRedirect

from .models import Questions
from .forms import AnswerForm

def index(request):
    question_list = Questions.objects.all()
    context = {
        'question_list': question_list
    }
    return render(request, 'pages/questions.html', context)


def answers(request):
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
    else:
        form = AnswerForm()
        
    return render(request, 'pages/questions.html', {'form': form})

Have you worked your way through the standard Django Tutorial yet? The way your code is written here is giving me the impression that you haven’t.

If you have, then I suggest you read the Working with Forms docs to get a better understanding of how forms work and how they’re related to views.

I literally did this just like in django documentation. but yes I am a beginner

Which documentation pages are you modelling this from?

Django forms widgets and working with forms

So I’ll go back to my earlier question, have you worked your way through the Django Tutorial?
(And by “working through”, I mean actually typing all the presented examples and exercises. The physical act of typing the code - and fixing the occasional mistake - greatly improves comprehension of the material. Just breezing through the material and copy/pasting it into an editor doesn’t provide nearly as much benefit.)

no I went through the tutorial and everything worked(and yes by going through I don’t mean just copy/pasting it. I want to learn for real and this is fun for me)

Cool, so then you’ll want to go back and review your work on the polls application - it’s a very close parallel to what you’re trying to do here. Pay particular attention to what data is being supplied to those views, what data is being retrieved from the database, what is being supplied to the templates, and which templates are being rendered for each page.

1 Like

Following this.
i am also working with Forms to get a better understanding of how forms work and how they’re related to views.