Doubt in Python request Django GET/POST

Asked

Viewed 756 times

0

Good evening, I have a problem that I cannot solve in Python 3.6.7 and Django 2.1.1

I have the following codes:

views

@login_required
def profile_provider(request, provider_id):
    provider = get_object_or_404(Provider, id=provider_id)
    form_contact = ContactForm()
    return render(request, 'provider/profile_provider.html', {'provider' : provider , 'form_contact' : form_contact})

@login_required
def add_contact_to_provider(request, provider_id):
    print(request.method)
    provider = get_object_or_404(Provider, id=provider_id)
    if request.method == 'POST':
        create_contact(request)
        provider.contacts.add(form_contact)
        print('Contato adicionado com sucesso')
    return redirect('purchasing_department:provider:profile_provider', provider_id)

@login_required
def create_contact(request):
    if request.method == 'POST':
        form_contact = ContactForm(request.POST)
        if form_contact.is_valid():
            print('Contato criado com sucesso')
            return form_contact.save()
    raise Http404("Erro ao cadastrar novo contato")

template

<form method="POST">
                {% csrf_token %}
                <div class="row">
                    <div class="col">
                        <div class="collapse multi-collapse" id="contact">
                            <div class="card card-body">
                                <div class="form-group">
                                    <label for="{{ form_contact.name.id_for_label }}">Nome</label><br>
                                    {{ form_contact.name }}
                                </div>
                                <div class="form-group">
                                    <label for="{{ form_contact.phone_number.id_for_label }}">Telefone</label><br>
                                    {{ form_contact.phone_number }}
                                </div>
                                <div class="form-group">
                                    <label for="{{ form_contact.phone_type.id_for_label }}">Tipo</label><br>
                                    {{ form_contact.phone_type }}
                                </div>
                                <div class="form-group">
                                    <span class="float-left">
                                        <a href="{% url 'purchasing_department:provider:add_contact_to_provider' provider.id %}" style="margin: 0 5px;" data-toggle="tooltip" type="button" class="btn btn-info">Adicionar</a>
                                    </span> 
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

urls

path('perfil/<int:provider_id>', views.profile_provider, name='profile_provider'),
path('adicionar-contato/<int:provider_id>', views.add_contact_to_provider, name='add_contact_to_provider'),

  1. The problem

So far so good, the only problem is that the request is being made with the GET Method, I want it to be done with the POST method. How do I modify the request for POST?

[11/Feb/2019 02:17:38] "GET /departamento-de-compras/fornecedor/perfil/1 HTTP/1.1" 200 13592
GET (perceba-se que esse é o print(request.method))
[11/Feb/2019 02:17:40] "GET /departamento-de-compras/fornecedor/adicionar-contato/1 HTTP/1.1" 302 0
[11/Feb/2019 02:17:40] "GET /departamento-de-compras/fornecedor/perfil/1 HTTP/1.1" 200 13592
[11/Feb/2019 02:20:33] "GET /departamento-de-compras/fornecedor/perfil/1 HTTP/1.1" 200 13606

Attention to detail in the.html template

<form method="POST">

1 answer

2

To submit a POST type request you need to submit a form.

Your form has an "a" link as the "add" button".

Instead of using:

<a type="button">Adicionar</a>

One should place:

<button type="submit">Adicionar</button>

Yes, you will have a button that will submit the form sending a POST.

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.