URL patterns in Django 2
Asked Answered
B

2

6

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?

Ballentine answered 21/5, 2018 at 17:21 Comment(6)
It doesn't matter which version of Django you use, query string values are still not part of the URL.Older
Hi @DanielRoseman, this is not actually a query string, I just need to grab a search term from the user to use it for Github API.Ballentine
That is exactly what a querystring is.Older
So, is there any way I can get a string from users in a url?Ballentine
Well it is part of the GET parameters.Elocution
yes, it's the part of the GET.Ballentine
D
9

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').

Dorathydorca answered 21/5, 2018 at 17:29 Comment(0)
A
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)
Accountancy answered 22/12, 2020 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.