How can I send a message when I redirect to the page?

0

my question is how can I do so that if a record is inserted correctly, when redirecting me it shows me the registration message inserted correctly.

This is the view that redirects me:

@login_required()
def inventarioempaque(request):
    empaque = Empaque.objects.all().order_by('fecha' , 'celda')
    for obj in empaque:
        delta = fecha_actual - obj.fecha
        obj.dias_desde_registro = delta.days
    #Codigo que retorna la plantilla home y llama el diccionario context
    return render(request,"inventario/empaque/empaque.html",{'empaque': empaque});

This is the view that adds the product:

def inventarioingresoe(request):
    empaque = Empaque.objects.all()
    if request.method == "POST":
        variedad = request.POST["variedad"]
        empaque = request.POST["empaque"]
        grado = request.POST["grado"]
        comercializadora = request.POST["comercializadora"]
        ramos = request.POST["ramos"]
        unidades = request.POST["unidades"]
        celda = request.POST["celda"]
        if Empaque.objects.filter(celda=celda):
            return HttpResponse(json.dumps({'valido': False}))
        else:
            nuevoingreso = Empaque.objects.create( fecha = fecha,
                                                   hora = hora,
                                                   variedad = variedad,
                                                   empaque = empaque,
                                                   grado = grado,
                                                   comercializadora = comercializadora,
                                                   ramos = ramos,
                                                   unidades = unidades,
                                                   total = int(unidades) * int(ramos),
                                                   celda = celda)
            return redirect('empaque')

    return render(request,"inventario/empaque/ingresoe.html", {})

These are my url:

url(r'inventario/empaque/', SGregorio_views.inventarioempaque, name='empaque'),
    url(r'^inventario/ingreso/$', SGregorio_views.inventarioingresoe, name='empaqueingreso'),

Someone to help me please.

    
asked by Angie Lizeth Santiago Piñeros 30.09.2016 в 19:40
source

1 answer

0

When you save your registration, you can use Django Messages:

In your views.py

...
# En la parte donde haces tus imports
from django.contrib import messages


# al momento de guardar tu modelo
   ...
       nuevoingreso = Empaque.objects.create(
           fecha=fecha, hora=hora, variedad=variedad,
           empaque=empaque, grado=grado,
           comercializadora=comercializadora,
           ramos=ramos, unidades=unidades,
           total=int(unidades) * int(ramos), celda=celda
       )
       # simplemente agregas esta linea de código
       messages.succes(request, 'se ha creado el empaque exitosamente')
   ...
...

It only remains in your template in which you want to receive the message to add something like this, (you must place it in the place where you want the message to be seen), remember that it is the template where you will do the redirection, if you do it in the other, you will not be able to see it

template.html

...
{% if messages %}
    {% for message in messages %}
        <div class="row">
           <div class="alert {% if message.tags == "success" %}alert-success {% else %}alert-danger{% endif %} alert-dismissible" role="alert">
              <button class="close" aria-label="close" data-dismiss="alert" type="button"><span aria-hidden="true">x</span></button>
               <p>{{ message|safe }}</p>
          </div>
        </div>
    {% endfor %}
{% endif %}
...
    
answered by 30.09.2016 в 22:36