In Django, how can i pass information to the server without showing in the link?

Viewed 339

i know we can pass information to the function that calls the template that way:

from django.urls import path
from . import views
urlpatterns = [
    path("domain/<str:u>/",views.pd)
]

in views.py:

from django.shortcuts import render

def rt(req,u):
    print(u)
    return render(req,'teste.html')

But how can we do this without showing the information in the link? For exemple, without 'http://localhost:8000/route/aaa/' in the link for passing the 'aaa' string

1 Answers

In general case, URL when sending post parameters doesnt contain anything related to parameters in the URL. To achieve that you would follow a similar approach as below.

For the html elements:

<input type='text' name='num' value=5 />

Urls.py

url(r'^register/$', 'register', name='urlname')

The corresponding view will be:

def register(request):
    if request.method == "POST":
        request.POST['num']  # returns 5
    return render(request, "template.html", context)

See this article here for a detailed rundown of how you can solve this problem.

Related