Home > Software design >  Form with post doesn't reach my function in django
Form with post doesn't reach my function in django

Time:01-12

I'm stuck with a st*pid question and problem.

I'm working with a form(html) on django, this form will be used to update information on my database regarding the page it's one.

So my problem is : django terminal tell me he receive the post correctly

app_1  | [10/Jan/2023 17:44:50] "GET /nb_database/49/ HTTP/1.1" 200 11810
app_1  | [10/Jan/2023 17:44:54] "POST /nb_database/49/ HTTP/1.1" 200 11810

However , i made a quick function to know my information from the form are coming to the function and here is the problem. The function never print anything, i supposed the request never reach the function

Here is the function (located on the app named 'nb_database' :

def adding_address_to_db(request):
    print("OK REQUETE RECEIVE")

the urls.py (on the same app):

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('nb_database', views.nb_database, name='nb_database'),
    path('nb_database/<int:annonce_id>/', views.detail, name='detail'),
    path('nb_database/<int:annonce_id>/', views.adding_address_to_db, name='adding_address_to_db'),
]

And here is the form in HTML , for this example let's say it's on page mysite.com/nb_database/49/

<form action="" method="post">
       {% csrf_token %} 
       <input type="text" name="adding_address_to_db" placeholder="Entrez l'adresse" style="color: black;">  
</form>

Basically i want to be able to answer the form on the generated page (mysite.com/nb_database/49/) and stay on it once answered.

Any help will be welcome ! Thank's everyone, enjoy your day

CodePudding user response:

You have 2 identical paths in urls.py. When you access mysite.com/nb_database/49/, only the function from the first path runs, not the second.
Remove this line and your function should run:

 path('nb_database/<int:annonce_id>/', views.detail, name='detail'),

CodePudding user response:

OK so , i resolved my problem.

As said previously you can't have 2 urls pointing to 2 differents function on view.

I had to use the class Form provided by django. Working with the post method and the form class i manage to add my function in the function who was generating the page and handle when a post method is send to catch the POST request and execute the code.

Here is a link to Django form documentation : Django Documentation

And this is the kind of code i have to add to my rendering function :


from django.http import HttpResponseRedirect
from django.shortcuts import render

from .forms import NameForm

def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})
  • Related