I have a form to upload locations, if I remove the image field of the models.py it is uploaded if not no! Would they know where they could have the error?
My models. py:
class Ubicacion(models.Model):
nombre = models.CharField(max_length=200)
lat = models.CharField(max_length=50)
lng = models.CharField(max_length=50)
imagen = models.ImageField()
fecha = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User,on_delete=models.PROTECT)
descripcion = models.CharField(max_length=500)
TextoParaAudio = models.CharField(max_length=100)
Ruta = models.ForeignKey(Ruta,on_delete=models.PROTECT)
My views:
ddef upload_file(request):
print("ii")
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES )
print("FIle:"+ str(request.FILES))
if form.is_valid():
m = Ubicacion.objects.get(pk=id)
m.model_pic = form.cleaned_data['imagen']
m.save()
return HttpResponse('image upload success')
else:
print("no entro")
return HttpResponseForbidden('allowed only via POST')
My index.html
{% extends 'base.html'%}
{% block title%}
Aplicacion en Django y Gmaps
{% endblock %}
{% block container%}
<div id="mapa" class="capas"></div>
<div id="datos" class="capas">
<div id="data">
<select id="cars" name="cars" size="10">
{% for ubicacion in ubicaciones %}
<option value="{{ ubicacion.id }}"> {{ ubicacion.nombre }} {{ ubicacion.user }} - hace {{ ubicacion.fecha | timesince }}</option>
{% endfor %}
</select><button type="button" id="deleteUbicacion">Eliminar</button>
</div>
<div id="form" >
<form method="post" id="form_coords" id="upload_file" enctype="multipart/form-data" >{% csrf_token %}
{{ form.as_p }}
<p>
<input type="submit" id="guardar" value="Guardar Ubicacion">
</p>
</form>
</div>
Thank you very much