Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 796
Abdul Rehman Avatar asked May 21 '18 17:05

Abdul Rehman


People also ask

What are URL patterns in Django?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.

What are URLs and views in Django?

URLs are the front door to your web application in Django. You can configure how this Django routing happens in urls.py. The views.py file is where you can define your view functions. These functions are what determine what the application does based on the URL that a user visits in a web browser.

How does URLs py work in Django?

A request in Django first comes to urls.py and then goes to the matching function in views.py. Python functions in views.py take the web request from urls.py and give the web response to templates. It may go to the data access layer in models.py as per the queryset.

What is a URL pattern?

A URL pattern is a set of ordered characters that is modeled after an actual URL. The URL pattern is used to match one or more specific URLs. An exception pattern starts with a hyphen (-). URL patterns specified in the Start and Block URLs page control the URLs that the search appliance includes in the index.


1 Answers

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

like image 88
SHIVAM JINDAL Avatar answered Nov 05 '22 07:11

SHIVAM JINDAL