Skip to content

FIle upload works in Admin panel but not through ModelForm #236

Open
@ProPegasus

Description

@ProPegasus

I have a model form that lets users upload images. But when we click submit on that form, it's not being uploaded into s3.
I have registered the same model in admin and the upload works through admin panel. So I know my s3 has been configured properly.
I have made it to return a Httpresponse called error when the form is invalid and it's returning that when I click subit
I am very new to django and can't wrap my head around this. Appreciate any help. Thank You

My model

class advert(models.Model):
    category_choices = (
        ("choice", "choice"),
    )
    name = models.CharField(max_length=32)
    part_name = models.CharField(max_length=50, blank=True)
    category = models.CharField(choices=category_choices, max_length=255)
    photo = S3DirectField(dest='primary_destination')
    seller = models.ForeignKey(User, on_delete=models.CASCADE)
    soldby = models.CharField(max_length=32, blank=True)
    place = models.CharField(max_length=32, blank=True)
    cell = models.CharField(max_length=13,blank=True)

My ModelForm

class advertform(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(advertform, self).__init__(*args, **kwargs)
        self.fields['seller'].widget.attrs['hidden'] = True
    soldby = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'}))
    place = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'}))
    cell = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly', 'class':'inpt'}))
    # photo = forms.URLField(widget=S3DirectWidget())
    name = forms.CharField(widget=forms.TextInput(attrs={'class': 'inpt', 'placeholder': 'Name of component'}))
    part_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'inpt', 'placeholder': 'Part no./name'}))
    class Meta:
        model = advert
        fields = ('name', 'part_name', 'category', 'photo', 'soldby', 'place', 'cell', 'seller')

My views function

def addpost(request):
    if not User.is_authenticated:
        return HttpResponse('Please Login')
    else:
        if request.method == 'POST':
            form = advertform(request.POST, request.FILES)
            if form.is_valid():
                advert = form.save()
                advert.refresh_from_db()
                advert.save()
                return HttpResponse('Advertisement added')
            else: return HttpResponse('Error')
        else:
            form = advertform(initial={'soldby': request.user.userprofile.store, 'place': request.user.userprofile.town,'cell': request.user.userprofile.phone,'seller': request.user.id})
        return render(request, 'adpost.html', {'form': form})

HTML

<form class="box2" id="form" method="POST" action="" enctype="multipart/form-data">
              {% csrf_token %}
              {{ form }}
              <button type="submit" id="btn">Publish Ad</button>
</form>

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions