[SOLUCIONADO] Ayuda con Django Python

Iniciado por Jonydakid, Diciembre 27, 2018, 06:31:56 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Diciembre 27, 2018, 06:31:56 PM Ultima modificación: Diciembre 28, 2018, 09:43:55 AM por Gabriela
Hola buenas a todos, esta es la primera consulta que hago en el foro, solo me he dedicado a leer los aportes xD pero bueno acá va.
Pasa que estoy intentando desarrollar una web ocupando Django Framework, en la cual en un formulario necesito subir una foto y me tira un error MultiValueDictKeyError.
Según yo es porqué la foto seleccionada no está pasando al servidor y lo digo por esto:


Tengo por códigos:
forms.py
Código: text

class FormProducto(forms.Form):
    CATEGORIAS = (
        ("PP", "Panes de Pascua"),
        ("EMP", "Empanadas"),
        ("PA", "Panes Amasados")
    )
    nombre = forms.CharField(label="Nombre", max_length=100, widget=forms.TextInput(
        attrs={"id": "nombre", "class": "campo"}))
    categoria = forms.ChoiceField(label="Categoria", widget=forms.Select(
        attrs={"id": "categoria", "class": "campo", "placeholder": "Categoria"}), choices=CATEGORIAS)
    precio = forms.IntegerField(label="Precio", min_value=0, widget=forms.NumberInput(
        attrs={"id": "precio", "class": "campo", "placeholder": "5000"}))
    stock = forms.IntegerField(label="Stock", min_value=0, widget=forms.NumberInput(
        attrs={"id": "stock", "class": "campo", "placeholder": "1"}))
    foto = forms.ImageField(
        label="Foto",required=False, widget=forms.ClearableFileInput(attrs={"id": "foto"}))
    descripcion = forms.CharField(label="Descripcion", max_length=255, widget=forms.TextInput(
        attrs={"id": "descripcion", "class": "campo"}))


view.py
Código: text

def addProducto(request):

if request.method == "POST":
form = FormProducto(request.POST,request.FILES)
if form.is_valid():
data = form.cleaned_data
Producto.objects.create(
nombre=data.get("nombre"),
categoria=data.get("categoria"),
precio=data.get("precio"),
stock=data.get("stock"),
descripcion=data.get("descripcion"),
foto=request.FILES["foto"])

return redirect('addProducto')
else:
form = FormProducto()
return render(request, "addProducto.html", { "titulo": "Añadir producto","form": form })


Espero puedan ayudarme ya que llevo unos días con este problema y más me cuesta aún siendo newbie xD
Muchas gracias de antemano!

PD:El archivo You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login ya está configurado

Si es un dictkey error es que te falta un atributo en tu formulario

Sent from my SM-G960F using Tapatalk


Te falta el.atributo foto wey en el formulario

Sent from my SM-G960F using Tapatalk


Si lo tienes disculpa en tapatalk no lo veia pero ya vi en la web haha hay lo recheko

Sent from my SM-G960F using Tapatalk


Como es tu from en la vista que estas pasadole al input de la foto

Sent from my SM-G960F using Tapatalk


You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Como es tu from en la vista que estas pasadole al input de la foto

Sent from my SM-G960F using Tapatalk
Con vista me refiero a que mandas desde el template no al You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login

Sent from my SM-G960F using Tapatalk


Estas intentando acceder a un atributo que no esta, en tu You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login ese es el error que te esta tirando

Sent from my SM-G960F using Tapatalk

Diciembre 27, 2018, 07:47:40 PM #7 Ultima modificación: Diciembre 27, 2018, 07:49:28 PM por Jonydakid
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Estas intentando acceder a un atributo que no esta, en tu You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login ese es el error que te esta tirando

Sent from my SM-G960F using Tapatalk

Código: text

{% extends "maqueta.html" %}


{% block Content %}
<h1>Página para añadir producto</h1>

<form id="addProducto" method="POST" enctype="multipart/form-data">
{%csrf_token%}
<table class="tableform">
{% for campo in form %}
<tr>
<th width="20%">{{ campo.label_tag }}</th>
<td width="60%">{{ campo }}</td>
<td width="20%"><p id="error_{{ campo.id_for_label }}" class="error">{{ campo.errors }}</p></td>
</tr>
{% endfor %}
</table>
<input type="submit" class="campo boton" value="Añadir" align="center" />
</form>
{% endblock %}


El atributo no es la que está en el You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login?

Si pero el error es al recuperar el dict que viene dentro del POST que es un diccionario y estas intentando acceder a un atributo de tu form que no existe o por lo menos eso es lo que parece

Sent from my SM-G960F using Tapatalk


You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Si pero el error es al recuperar el dict que viene dentro del POST que es un diccionario y estas intentando acceder a un atributo de tu form que no existe o por lo menos eso es lo que parece

Sent from my SM-G960F using Tapatalk

No entiendo. No se supone que ese pasa con el attrs={"id": "foto"} del You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login? :o

Diciembre 28, 2018, 12:30:50 AM #10 Ultima modificación: Diciembre 28, 2018, 12:36:31 AM por dannybombastic
No, es name no id jaja

Sent from my SM-G960F using Tapatalk

You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
No, es name no id jaja

Sent from my SM-G960F using Tapatalk
eso explica todo.
Muchas gracias!

You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
No, es name no id jaja

Sent from my SM-G960F using Tapatalk
eso explica todo.
Muchas gracias!
Jajaj eso nos pasa a cualquiera pues a  seguir buena suerte y ya sabes todos juntos somos mas fuertes un saludote desde malaga

Sent from my SM-G960F using Tapatalk