0

i am trying to upload a image from a form that i am building.

As i said in the title, my error is the file is not uploaded. And i dont know where is the mistake, here is my models.py:

def upload_location(instance, filename):
    return "uploads/%s/img/%s/" % (instance.id, filename)

class CustomUser(AbstractBaseUser, PermissionsMixin):
    ...
    width_field      = models.IntegerField(default=0)
    height_field     = models.IntegerField(default=0)
    photo            = models.ImageField(
                                        upload_to=upload_location,
                                        null=True, blank=True, 
                                        width_field="width_field", 
                                        height_field="height_field"
                                    )


    USERNAME_FIELD  = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    objects = UserManager()

    ...

The problem is the "photo" field.

Here is form where i am calling this:

class UserConfigurationForm(UserChangeForm):

    class Meta:
        model=CustomUser

        fields = (
                    'photo',
                    'password'
                    ...
                 )

My template where i am calling the form:

                  <form method="POST">
                    {%csrf_token%}

                    <input type="file" name="photo" accept="image/*">
                    ...

            <!--SUBMIT-->
                      <div class="form-group">
                           <div class="col-xs-12">
                                <br>
                                <button class="btn btn-lg btn-success" type="submit"><i class="glyphicon glyphicon-ok-sign"></i> Guardar cambios</button>
                      </div>
                </form>

Finally, this is my views.py:

def configuration(request):

    form = UserConfigurationForm(request.POST or None, instance=request.user)

    if form.is_valid():

        form.save()
        return redirect('myuser:myuser')

    else:
        form = UserConfigurationForm(instance=request.user)
        return render(request, 'myuser/configuration.html', {'form': form})

So, if you can tell me where is my mistake from all of this, i´ll be appreciated.

Thank you.

Luis Bermúdez
  • 592
  • 11
  • 25
  • 1
    https://docs.djangoproject.com/en/2.2/topics/http/file-uploads/#basic-file-uploads – Ivan Starostin Jun 14 '19 at 16:49
  • Yes, but i have another fields apart from "photo". And i am using another method of uploading files, so this wont work for me. Any suggestions? :) – Luis Bermúdez Jun 14 '19 at 16:51
  • `i have another fields apart from "photo"` - and? `i am using another method of uploading files` - other then posting a form? Have you ever looked at the docs? There are several straightforward and complete examples of how to upload files. Any of them clearly defines what's wrong with your view. – Ivan Starostin Jun 14 '19 at 17:03

2 Answers2

1

First of all add in form tag in your HTML code add this property:

enctype="multipart/form-data"

Then you should get Data from your form in your view which include your image data too:

if request.method == 'POST':
    form = UserConfigurationForm(request.POST, request.FILES, request.user)
    if form.is_valid():
        ...
        handle_uploaded_file(request.FILES['photo']) # this will handle you image
        return HttpResponseRedirect('/success/url/')
else:
    ...
return render(request, 'upload.html', {'form': form})
Ali Akhtari
  • 1,211
  • 2
  • 21
  • 42
  • Can you explain me this with more details? i put the enctype. But i dont understand the part of handle_uploaded_file(request.FILES['photo']) # this will handle you image return HttpResponseRedirect('/success/url/') – Luis Bermúdez Jun 18 '19 at 02:12
  • 1
    in your view.py you receive a request which include you html form data, this data is passed by request, so when you want to seprate you request's data, you have to handle your request data, in this case, you request include a file, so you should use `request.File[' Your HTML input Name tag ']` to receive and get your file. you can read full tutorial and documentation in the link below: [Django File Uploads](https://docs.djangoproject.com/en/2.2/topics/http/file-uploads/) – Ali Akhtari Jun 18 '19 at 14:43
  • 1
    and about `enctype="multipart/form-data"` check the link below to understand it completely : [What does enctype='multipart/form-data' mean?](https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) – Ali Akhtari Jun 18 '19 at 14:46
0

U forgot to send the FILES from the request to the form

def configuration(request):

    form = UserConfigurationForm(request.POST or None,request.FILES, instance=request.user)

    if form.is_valid():

        form.save()
        return redirect('myuser:myuser')

    else:
        form = UserConfigurationForm(instance=request.user)
        return render(request, 'myuser/configuration.html', {'form': form})
Walucas
  • 2,549
  • 1
  • 21
  • 44