简体   繁体   English

Django 2 中的 URL 模式

[英]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我刚刚使用 Django 2.0 开始了我的第一个项目,其中我需要以如下方式定义 URL: 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但我不知道如何在 Django 2.0 中为 URL 定义字符串参数

Here's what I have tried:这是我尝试过的:

From ulrs.py:来自 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.无需在 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') .让你调用 URL http://localhost:8000/navigator/?search_term=arrow然后你可以通过request.GET.get('search_term')得到 search_term 。

Request: GET请求:获取

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

urls.py网址.py

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

views.py视图.py

search_term = request.GET.get('search_term', None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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