python - 在 Django 中重定向到带有查询字符串的 url

标签 python django

如果我有 View ,如何使用参数转到特定的 URL

def search(request):

在 urls.py 中

^search/$ 

我需要做的是像 search/?item=4 那样重定向

c = {}
render_to_response("search.html",c) 

工作正常,但是

render_to_response("search.html/?item=" + itemID, c )

它说没有找到模板(我知道没有像 search.html/?item= 这样的模板)但是我如何传递参数或使用查询字符串进行重定向?

最佳答案

使用反向并传递 url 的名称,我们可以重定向到带有查询字符串的 url:

#urls.py

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

#views.py

from django.shortcuts import redirect, reverse

# in method
return redirect(reverse('search_view') + '?item=4')

关于python - 在 Django 中重定向到带有查询字符串的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784810/

相关文章:

python - 通过 HttpResponse (PyPDF2) 在 Azure 函数中返回 PDF

python - python 3中类 'bytes'的不同表示

Python 在工作目录中打开 .txt 文件/更改时出现错误消息

Django request.is_secure 为重定向方法返回错误值

django - 使用 Django Rest Framework 将扁平 JSON 转换为嵌套 JSON

python - python中有 "too many yield statements"这样的东西吗?

python - 有没有办法在kivy中录制视频?

python - Django 脆皮表格 : Exception while resolving variable 'form_html' in template 'bootstrap3/whole_uni_form.html'

python - Django:如何为登录用户数据的查询集编写 Mixin

python - Django 修改或重置迁移