2

My current issue is that no form shows and if I add {{ form.as_p }} to the create.html page than only the TradeForm shows and without any images.

I have used this question for reference: how to upload multiple images to a blog post in django

views.py

class TradeCreateView(CreateView):
    template_name = "tj/cp/trade/create.html"
    form_class = TradeForm
    form_class_image = ImageForm
    queryset = Trade.objects.all()

    def post(self, request, *args, **kwargs):

        ImageFormSet = modelformset_factory(Image, form=ImageForm, extra=3)
        formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none())

        if TradeForm.is_valid() and formset.is_valid():
            trade_form = TradeForm.save(commit=False)
            trade_form.user = request.user
            trade_form.save()

            for form in formset.cleaned_data:
                image = form['address']
                photo = Image(trade=trade_form, image=image)
                photo.save()


    def form_valid(self, form):
        print(form.cleaned_data)
        return super().form_valid(form)

forms.py

class TradeForm(forms.ModelForm):
    class Meta:
        model = Trade
        fields = [
            'user',
            'target_size',
            'target_entry',
            ...
        ]

        # exclude = ['user',]

class ImageForm(forms.ModelForm):
    address = forms.ImageField(label='Image')

    class Meta:
        model = Image
        fields = ('address',)

models.py

class Trade(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
    comments = models.TextField(max_length=10000, blank=True, null=True)

def get_image_filename(instance, filename):
    title = instance.post.title
    slug = slugify(title)
    return "uploads/%s-%s" % (slug, filename)


class Image(models.Model):
    trade = models.ForeignKey(Trade, blank=True, null=True, default=None, on_delete=models.CASCADE)
    address = models.ImageField(upload_to='uploads', blank=True, null=True, verbose_name='Image')
    title = models.CharField(max_length=100, null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE,  null=False)

    def __unicode__(self):
        return self.title

create.html

        <form id="TradeCreateView" method="post" action="." enctype="multipart/form-data">

            {% csrf_token %}


            {{ formset.management_form }}

            {% for form in formset %}
                {{ form.as_p }}
            {% endfor %}


            <input type="submit" name="submit" value="Submit" />
        </form>
Alex Winkler
  • 469
  • 4
  • 25
  • Hey, were you able to solve your problem? If so can you please share your solution? It will be really helpful. – fpaekoaij Jan 21 '20 at 17:40
  • I actually have not.. what I did instead was use the django-Ckeditor rich textform as my imagine uploader. I know this will probably not be ideal for most cases but for me it was fine. – Alex Winkler Jan 22 '20 at 09:30
  • Any updates about this question please. I'm also facing the same problem. –  Apr 20 '22 at 13:12
  • Still using the fix I made in the above comment. Will update if I switch solution @AdamuAbdulkarim – Alex Winkler Apr 29 '22 at 11:16
  • No worry Alex post the answer if you get it. –  Apr 29 '22 at 15:24

0 Answers0