Django Forms does not submit radiobutton value and does not showing any output in terminal as well

This is HTML Code.

<form action = "." method = "post">
    <div class="form_data">

        {% csrf_token %}
         <br><br>

        {{form.myfield}}
<br><br>
     <input type="submit" value="Submit" class="btn btn-success" />
        </div>
</form>

This is forms.py code

class TestForm(forms.ModelForm):
    class Meta:
        model = TestModel
        fields = "__all__"
        widgets = {'myfield': forms.RadioSelect()}

This is models.py code

class TestModel(models.Model):
    git_Id = models.CharField(max_length=200)
    git_Response = models.CharField(max_length=200)
    is_approved = models.IntegerField()
    MY_CHOICES = (
        ('opt0', 'Approved'),
        ('opt1', 'Not Approved'),
    )
    myfield = models.CharField(max_length=10, choices=MY_CHOICES, default="N/A")


views.py code

def test(request):
    if request.method == "POST":
        form = TestForm(request.POST)
        if form.is_valid():
            print("Form is Valid")
            selected = form.cleaned_data['myfield']
            print(selected)

            if selected == 'opt0':
                from config import request_id as r
                rq = r["request_id"]
                print(rq)
                s = sql()
                query = f"""update request_form_mymodel
                                       set is_approved=1
                                       where request_id = '{rq}' """
                print(query)
                s.update_query(query)
            else:
                pass

    else:
        form = TestForm()
    return render(request, 'test.html', {'form': form})

I am not getting any output, if i try to submit after selecting radio button then it does not working and not printing any variables values in terminal as well and form is not submitted. What I want - I want to getting form is submitted and if radiobutton is selected opt0 then s.update() is called.

The form is not going to the test view as you specified . in action attribute, kindly remove action attribute, since Django always takes current page route, so it will automatically go to test view so:

<form method="POST">
    <div class="form_data">

        {% csrf_token %}
         <br><br>

        {{form.myfield}}
<br><br>
     <input type="submit" value="Submit" class="btn btn-success" />
        </div>
</form>
Back to Top