5

I just have started my first project by using Django 2.0 in which I need to define a URL in a way as: http://localhost:8000/navigator?search_term=arrow

But I couldn't know how to define a string parameter for a URL in Django 2.0

Here's what I have tried:

From ulrs.py:

from Django.URLs import path from. import views

urlpatterns = [
    path('navigator/<str:search_term>', views.GhNavigator, name='navigator'),

]

Any help?

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150

2 Answers2

8

There is no need to define query params in URL. Below url is enough to work.

path('navigator/', views.GhNavigator, name='navigator')

Let you called URL http://localhost:8000/navigator/?search_term=arrow then you can get search_term by request.GET.get('search_term').

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
SHIVAM JINDAL
  • 2,844
  • 1
  • 17
  • 34
0

Request: GET

http://localhost:8000/navigator?search_term=arrow

urls.py

urlpatterns = [
    path('navigator/', views.GhNavigator, name='navigator'),
]

views.py

search_term = request.GET.get('search_term', None)
Hasan Khan
  • 633
  • 1
  • 8
  • 15