我正在尝试制作发布选项,所以我使用此
class Article(models.Model):
publish_options = models.CharField(max_length=50)
现在,在我的表单中,我使用了forms.CheckboxSelectMultiple widget
。所以我有这个
PUBLISH_VISIBILITY = (
('All', 'All'),
('Paid-users', 'Paid-users'),
('Free Users', 'Free Users'),
('Public', 'Public'),
)
class PortalNoteForm(ModelForm):
publish_options = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=PUBLISH_VISIBILITY)
class Meta:
model = Article
现在在我看来,我得到了像这样的复选框的值
if request.method == 'POST':
form = PortalNoteForm(request.POST)
if form.is_valid():
school_article = form.save(commit=False)
school_article.publish_options = form.cleaned_data['publish_options']
school_article.school_creator = admin
school_article.save()
return HttpResponseRedirect(reverse('going to somewhere'))
else:
form = PortalNoteForm()
context = {'form':form, 'notes':notes}
好的,这可以很好地保存publish_option的值,但作为列表,即使您只选择一个复选框,其值也会作为列表。现在问题是我无法阻止这些publish_options
值显示为django模板中的列表。我试过迭代它们但没办法。我真的需要帮助。
答案 0 :(得分:0)
您已经有了表单,为什么还要从/usr/bin/php
获取数据?表格假设采用request.POST
并将数据转换为更方便的方式供您使用:
views.py
request.POST