我正在尝试从imageField
上传图片,但是当我尝试上传时却什么也没上传。
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()
forms.py
class UserConfigurationForm(UserChangeForm):
class Meta:
model=CustomUser
fields = ('first_name', 'last_name', 'email', 'phone_number',
'direction', 'password', 'photo',)
views.py
def configuration(request):
categoria = Clasificacion.objects.filter(existencia=True)
templates = Templates.objects.get(isSelected=True)
if request.method == 'GET':
form = UserConfigurationForm(instance=request.user)
else:
form = UserConfigurationForm(request.POST or None, request.FILES or None, instance=request.user)
if form.is_valid():
form.save()
return redirect('myuser:myuser')
return render(request, 'myuser/configuration.html', {'form': form, 'categoria':categoria,'templates':templates})
模板
<form method="post" action='' enctype="multipart/form-data">
{%csrf_token%}
<div class="col-sm-8 ">
<strong>{{form.photo}}</strong></div>
<!--FIRST NAME-->
<label for="first_name">Nombre</label>
<div class="form-group">
<input class= "form-control" type="text" name="first_name" maxlength="20" value="{{user.first_name}}" required>
</div>
<label for="last_name">Apellidos</label>
<div class="form-group">
<input class="form-control" type="text" name="last_name" value="{{user.last_name}}">
</div>
<!--Username and email is same in this case-->
<label for="last_name">Correo electrónico</label>
<div class="form-group">
<input class= "form-control" type="email" name="email" value="{{user.email}}" required>
</div>
<!--Phone number-->
<label for="phone_number">Teléfono</label>
<div class="form-group">
<input class= "form-control" type="tel" pattern="[0-9]{4}-[0-9]{3}-[0-9]{4}" name="phone_number" placeholder="Formato ejem: 0414-685-4716" value="{{user.phone_number}}"required>
</div>
<!--Direction-->
<label for="direction">Dirección</label>
<div class="form-group">
<textarea id="direction" class= "form-control" rows="6" name="direction"
value="{{user.direction}}" required>{{user.direction}}</textarea>
</div>
<!--SUBMIT-->
<div class="form-group">
<div class="col-xs-12">
<br>
<button type="submit" class="btn btn-lg btn-success">Guardar cambios</button>
</div>
</form>
我以前做过,但是我不知道是什么原因造成的。其他领域工作正常,我只是照片有问题。我放了enctype
和request.FILES
,但是它不起作用。
希望您能帮助我!谢谢!