variable from template to view

12 views
Skip to first unread message

Luca Bertolotti

unread,
Sep 13, 2019, 11:12:43 AM9/13/19
to Django users
Hello
i have a question:
This is the template:
{% load static %}
<head>
<link rel="stylesheet" href="{%static "regole.css" %}">
</head>
<div id= "taba">
    <table>
        
        <tr class="prima">
            <td>ID</td> <td>Ragione Sociale</td> <td>Via</td> <td>Cap</td> <td>Città</td> <td>Provincia</td> <td>Stato</td> <td>Contatto</td> <td>Tel.Fisso</td> <td>Tel.Cellulare</td> <td>Email</td>
        </tr>
        
        {% for dati in tutti %}
                <tr class={% cycle 'odd' 'even' %}>
            <td>{{ dati.idclienti }}</td> <td>{{ dati.ragione}}</td> <td>{{ dati.via }}</td> 
            <td>{{ dati.cap }}</td> <td>{{ dati.citta }}</td> <td>{{ dati.prov }}</td> 
            <td>{{ dati.stato }}</td> <td>{{ dati.persona }}</td> <td>{{ dati.tel }}</td> 
            <td>{{ dati.cell }}</td> <td>{{ dati.email }}</td>
            <td><form action="canc_cli/{{ dati.idclienti }}" method="post"> {% csrf_token %}<button type="submit">Delete</button></form></td>
            </tr>
        {% endfor %}
    </table> 
</div>


When i click kon the button he call the funciorn canc_cli from the view file:

from django.shortcuts import render
from .models import Clienti

def tutti_clienti(request):
    tutti = Clienti.objects.all()
    return render(request, "polls/tutti_clienti.html", {"tutti": tutti})

def canc_cli(request, idcliente):
    print('luca :'+str(idcliente))
    Clienti.objects.filter(pk=idcliente).delete()
    tutti = Clienti.objects.all()
    return render(request, "polls/tutti_clienti.html", {"tutti": tutti})

Than he to the right job, but if i push the button again he search for the url "canc_cli/canc_cli/{{ dati.idclienti" the is not un my urls the problem is because when i do the first time the button function he never refereah the url in the browser instead of tutti_clienti.html in the browser i have canc_cli/idcliente 

Can you help me to solve the problem

Luca Bertolotti

unread,
Sep 13, 2019, 11:39:07 AM9/13/19
to Django users


Hello again
I have solved with this:

def canc_cli(request, idcliente):
    print('luca :'+str(idcliente))
    Clienti.objects.filter(pk=idcliente).delete()
    return redirect('tutti_clienti')
Reply all
Reply to author
Forward
0 new messages