单选按钮未显示,而是显示复选框

时间:2017-05-22 13:18:34

标签: python django django-forms django-views django-class-based-views

此表单在基于函数的视图中显示单选按钮,但在我介绍基于类的视图时更改为复选框,可能是什么解决方案这个。我希望他们再次显示收音机按钮

forms.py

class ProductImagesForm(forms.ModelForm):
media = forms.ImageField(label='Image')
featured_image = forms.BooleanField(initial=True) 

def __init__ (self, *args, **kwargs):
    super(ProductImagesForm, self).__init__(*args, **kwargs)
    self.fields['featured_image'] = forms.BooleanField( widget = forms.RadioSelect(attrs={'checked': 'true'}, choices=((self.prefix, 'featured'),)))

models.py

def product_download(instance, filename):
return '%s/%s' %(instance.product.slug, filename)

class ProductImages(models.Model):
    product = models.ForeignKey(Product)
    title = models.CharField(max_length=120)
    media = models.ImageField(upload_to=product_download, 
       width_field='max_width', 
       height_field='max_height',
       null=True, blank=True)
    max_width = models.CharField(max_length=100, null=True, blank=True)
    max_height = models.CharField(max_length=100, null=True, blank=True)
    featured_image = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
       return unicode(self.media)

class Meta:
    verbose_name = "product image"
    verbose_name_plural = "product images"

模板

<form enctype="multipart/form-data" action="" method="post"> {% csrf_token %} 
{{ form.as_p }}
{{ formset.management_form }}
<div class="link-formset">
{% for obj in formset %}
{{  obj.as_p }}
{% endfor %}
</div>

views.py

def get(self, *args, **kwargs):
    self.object = self.get_object()
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    formset = ImagesFormset(queryset=ProductImages.objects.none())
    return self.render_to_response(self.get_context_data(form=form, formset=formset))

def post(self, request, *args, **kwargs):
    self.object = self.get_object()
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    formset = ImagesFormset(self.request.POST, self.request.FILES)
    form_valid = form.is_valid()
    formset_valid = formset.is_valid()
    if form_valid and formset_valid:
        seller = self.get_account()
        form.instance.seller = seller
        self.object = form.save()
        media = formset.save(commit=False)
        for img in media:
            img.product = self.object
            img.save()
        formset.save()
        return self.form_valid(form, formset)
    else:
        return self.form_invalid(form, formset)`

1 个答案:

答案 0 :(得分:1)

经过一些很好的研究和敏锐的文档阅读后,我明白了这一点,我希望它能帮助其他人呢

forms.py

   class ProductImagesForm(forms.ModelForm):
        media = forms.ImageField(label='Image')
        featured_image = forms.BooleanField(initial=True)

        def __init__ (self, *args, **kwargs):
            super(ProductImagesForm, self).__init__(*args, **kwargs)
            self.fields['featured_image'] = forms.BooleanField(queryset=ProductImages.objects.all(), widget = forms.RadioSelect( attrs={'checked': True}, choices=((self.prefix, 'featured_image')),))

        def add_prefix(self, field):
            if field == 'featured_image': return field
            else: return self.prefix and ('%s-%s' % (self.prefix, field)) or field

        class Meta:
            model = ProductImages
            fields = ['media', 'featured_image']

    ImagesFormset = modelformset_factory(ProductImages, fields=('media', 'featured_image'), extra=5)

模板

{{ formset.management_form }}
<div class="link-formset">
{% for choice in formset %}

<div>
  {{ choice.media }}
 <input type="radio" name="{{choice.featured_image.label}}">{{ choice.featured_image.label }}</
</div>
{% endfor %}
</div>