简体   繁体   中英

URL patterns in Django 2

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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM