Goncalves
Goncalves

Reputation: 179

Django : Transfer data to another view by a button

I have a table of clients where each line has a button to redirect to another page with the id of the client. I do not want that it is on the URL, so I want to avoid a GET method, it is why I thought to us a POST method with the buttons.

I did something but it is not working, I have an error message "Method Not Allowed (POST)" with the specific URL.

html

<table id="customers"  class="table table-sortable">
            <tr>
                <th>Référence Client</th>
                <th>Client</th>
                <th>Edit</th>
            </tr>
            {% for client in query_list_client %}
                <tr>
                    <td>{{ client.id }}</td>
                    <td>{{ client }}</td>
                    <td>
                        <a href="{% url 'modifyclient' uuid_contrat uuid_group % }" class="btn btn-primary" >Modify</a>
                        <form action="{% url 'selectsaletype' uuid_contrat uuid_group %}" method="POST">
                            {% csrf_token %}
                            <button name = 'client' type ='submit' value="{{ client.id }}" class='btn btn-secondary' > Select </button>
                        </form>

                        <!-- <a href="{% url 'selectsaletype' uuid_contrat uuid_group client.id % }" class="btn btn-secondary" >Selectionner</a> -->
                    </td>
                </tr>
            {% endfor %}
        </table>

urls

urlpatterns = [ 
    path('<str:uuid_contrat>/<str:uuid_group>/sales/selectclient/', ListSelectClient.as_view(), name="selectclient"),
    path('<str:uuid_contrat>/<str:uuid_group>/sales/selectsaletype/', SelectSaletType.as_view(), name="selectsaletype"),

   ]
views
class SelectSaletType(ListView):
    model = ListSaleType
    paginate_by =10
    template_name = 'sales/selectsaletype.html'
    context_object_name = 'query_sale_type'

    def get_context_data(self, *args, **kwargs) :
    
        context = super(SelectSaletType, self).get_context_data(*args, **kwargs)
        context['uuid_contrat'] = self.kwargs['uuid_contrat']
        context['uuid_group'] = self.kwargs['uuid_group']

        print(self.request.POST)

        return context

    def get_queryset(self):
        
        # Get the UUID_CONTRAT
        uuid_contrat = self.kwargs["uuid_contrat"]
        id_contrat = Entity.objects.all().filter(uuid=uuid_contrat).values_list('id', flat=True)[0]

        query = self.request.GET.get('search')

        if query:
            query_sale_type = self.model.objects.filter(Q(idcustomer=id_contrat)).order_by('-id')
        else:
            query_sale_type = self.model.objects.all().filter(idcustomer=id_contrat).order_by('-id')

        return query_sale_type

Upvotes: 1

Views: 533

Answers (1)

nigel222
nigel222

Reputation: 8212

IT has to be stored between views. The server is stateless. Options are in a browser cookie or in the DB, if you rule out putting it in the URL or in a GET parameter.

Django provides the session (via middleware) which can store things in either cookies or the DB depending on configuration. So use that ( request.session )

Upvotes: 1

Related Questions