Top of the morning to you.
I'm trying to build a kinda-sorta e-voting system where there are positions and certain aspirants vie for said positions.
Now I have a form setup with a radio Choice() widget, but I want the choices dict [CHOICES] to be dynamic per page, that is, the list only contains the aspirant going for that particular position.
I intend doing this by gettting the urls and comparing them with the absolute url of each aspirant. But I have trouble doing this, I can't seem to succesfully get the request object to work.
I'll appreciate any help.
Cheers,
'Kayode
My forms.py
----------------------------------forms.py-------------------------------------------------------------
from django import forms
from django.http import Http404, HttpRequest
from .models import Aspirant, Position
class VotingForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(VotingForm, self).__init__(*args, **kwargs)
def get_url(self):
_ = []
for post in Position.objects.all():
if self.request.get_full_path() == "/post/pro":
no_ = Position.objects.get(post='PRO')
_.clear()
for i in Aspirant.objects.filter(post=no_):
_.append(tuple([i.name, i.name]))
elif self.request.get_full_path() == "/post/gen-sec":
no_ = Position.objects.get(post='General Secretary')
_.clear()
for i in Aspirant.objects.filter(post=no_):
_.append(tuple([i.name, i.name]))
elif self.request.get_full_path() == "/post/vp-admin":
no_ = Position.objects.get(post='Vice President (Admin)')
_.clear()
for i in Aspirant.objects.filter(post=no_):
_.append(tuple([i.name, i.name]))
elif self.request.get_full_path() == "/post/vp-editorial":
no_ = Position.objects.get(post='Vice President (Editorial)')
_.clear()
for i in Aspirant.objects.filter(post=no_):
_.append(tuple([i.name, i.name]))
elif self.request.get_full_path() == "/post/president":
no_ = Position.objects.get(post='President')
_.clear()
for i in Aspirant.objects.filter(post=no_):
_.append(tuple([i.name, i.name]))
return _
aspirants = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
class Meta:
model = Aspirant
fields = ['aspirants']