Do not upload the image in a django form

1

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

    
asked by Marta 13.04.2018 в 11:35
source

4 answers

0

You need enctype=multipart/form-data on your label form . If the error persists, check your setting.py if the settings of media_url and media_root are correct. Just in case, you should also add this in url.py :

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
answered by 13.04.2018 в 11:43
0

I've looked at your code and I'm seeing this:

if form.is_valid():
    newdoc = Ubicacion(imagen=request.FILES['imagen'])
    newdoc.save(form)

If you have the form object loaded with the data that you have passed through the template and validated it, why do you load an object Ubicacion and pass this data and then call the function save() and pass the form (again).

Directly form.save() .

    
answered by 17.04.2018 в 10:54
0

You have the wrong form:

<div id="form" enctype=multipart/form-data>
<form method="post" id="form_coords" action="upload_file" >{% csrf_token %}
    {{ form.as_p }}
    <p>
        <input type="submit" id="guardar" value="Guardar Ubicacion">
    </p>
</form>

The enctype attribute goes inside the form tag.

    
answered by 17.04.2018 в 12:09
0

I found an example of code that can solve the problem you have with your view, upload images django

    
answered by 17.04.2018 в 12:22