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.