Как я могу динамически устанавливать выбор в моей форме? Эти варианты будут меняться в зависимости от маршрута, который выберет пользователь.
forms.py
class RegistrationForm(forms.Form):
options = ()
camp_dates = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=options)
Я хотел бы установить варианты из моего файла view.py так что я могу установить эти варианты динамически.
views.py
def camp_datailed_view(request,slug):
options = (
("1", "Jan"),
("2", "Feb"),
)
form = RegistrationForm()
##How can I pass options into the form field camp_dates as selectable choices
def register(request):
form = RegistrationForm()
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = RegistrationForm(request.POST)
# check whether it's valid:
if (form.is_valid()):
return render(request,'camp_registration.html')
else:
print (form.errors)
return HttpResponse("not working")``