Skip to main content
 首页 » 编程设计

python之使用参数 django python 重定向到 url

2024年08月28日27zhwl

迎角, 我正在尝试重定向以查看参数但无法获取参数

这是代码,如果我改用 (r'^search/$',... ) 就可以正常工作

url.py

url(r'^search/\?item=(?P<item_id>\d+)/$', 'contacts.views.search_Page', name='search_view'), 

views.py

def search_Page(request,item_id):                   #GET Method 
    return redirect('home_view') 

它没有给我任何错误,但 django 无法找到页面或 View (404 页面),为什么?

使用 TestApp.urls 中定义的 URLconf,Django 按以下顺序尝试这些 URL 模式:

^$ 
^home/$ [name='home_view'] 
^logout/$ 
^save/$ 
^edit/$ 
^create/$ 
^account/$ 
^callback$ [name='callback'] 
^profile/$ [name='profile_view'] 
^search/\?item=(?P<item_id>\d+)$ [name='search_view'] 
^get/(?P<article_id>\d+)/$ 
^static\/(?P<path>.*)$ 
The current URL, search/item=4, didn't match any of these. 

请您参考如下方法:

路径进入 urls.py。在 View 中处理查询参数。

url.py

url(r'^search/$', 'contacts.views.search_Page', name='search_view'), 

views.py

def search_Page(request): 
    address = request.GET['item'] # or GET.get('item', '') if the parameter might not exist 
    print address