【发布时间】:2019-04-08 11:55:14
【问题描述】:
我正在开发一个电子投票系统,在该系统中,有志于争夺特定职位的人。我正在尝试创建一个表单,其中在 RadioSelect 按钮中的每个位置显示有抱负的人。
为此,我尝试通过 Position() 类中的所有对象初始化 for 循环,并使用 if 语句将当前 URL 路径与每个对象的 get_absolute_url() 进行比较。 我无法让请求模块工作。
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):
self._ = []
for post in Position.objects.all():
if self.request.get_full_path() == "/post/pro":
no_ = Position.objects.get(post='PRO')
self._.clear()
for i in Aspirant.objects.filter(post=no_):
self._.append(tuple([i.name, i.name]))
elif self.request.get_full_path() == "/post/gen-sec":
no_ = Position.objects.get(post='General Secretary')
self._.clear()
for i in Aspirant.objects.filter(post=no_):
self._.append(tuple([i.name, i.name]))
return _
CHOICES = self.get_url()
aspirants = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
class Meta:
model = Aspirant
fields = ['aspirants']
我收到此错误。 我不确定我做错了什么。
CHOICES = self.get_url()
NameError: name 'self' 未定义
【问题讨论】: