0
from django import forms

class MyForm(forms.Form):
    checkbox = forms.MultipleChoiceField(
        choices=[('apple', 'apple'), ('orange', 'orange')],
        widget=forms.CheckboxSelectMultiple
    )

urls

from django.urls import path
from . import views

urlpatterns=[
    path('myform/',views.my_form,name='my_form')
]
views

from django.shortcuts import render
from aapp.forms import MyForm


# Create your views here.
def my_form(request):
    if request.method == 'POST':
        selected = request.POST.getlist('checkbox')
        print(selected)
    else:
        form = MyForm()
    return render(request, 'my_form.html', {'form': form})

urls

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('aapp.urls'))
]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button id="submit-btn" type="submit">Submit</button>
</form>

</body>
<

when i click checkbox apple - i need to show apple? How to fix it? Any other ways to print checkbox values in a template in Django-models-forms-templates?

Multiple checkbox value shows on templates when i click the value

0 Answers0